双向链表建立、插入删除
2018-03-20 10:13
218 查看
#include<stdio.h> #include<stdlib.h> typedef struct Node{ struct Node* pre;//指向上一个 int data; struct Node *next;//指向下一个 }STU,*nodes; nodes creatnew()//建立头结点 { nodes head; head=(nodes)malloc(sizeof(STU)); head->pre=NULL; head->next=NULL; return head; } nodes creat_list(nodes head)//创建链表 { int i,n; nodes p1,p2; p2=head; printf("请输入要创建的节点个数:"); scanf("%d",&n); for(i=0;i<n;i++) { p1=(nodes)malloc(sizeof(STU)); scanf("%d",&p1->data); p2->next=p1; p1->pre=p2; p2=p1; } p2->next=NULL; return p2; } void put(nodes head)//输出 { nodes p; p=head; while(p->pre!=NULL) { printf("%d\n",p->data); p=p->pre; } } nodes Delete(nodes head)//删除 { int num; nodes p1,p; p1=p=head; printf("请输入要删除的数据\n"); scanf("%d",&num); p=p->pre; do { if(p->data==num) { p1->pre=p->pre; p1=p; //free(p); return head; } else { p1=p; p=p->pre; } }while(p!=NULL); } nodes insert(nodes head)//插入 { int num; nodes p,p1,news; p=p1=head; printf("请输入要插入的数据:\n"); scanf("%d",&num); while(p!=NULL) { p1=p; p=p->next; if(p->data>num) { news=(nodes)malloc(sizeof(STU)); news->data=num; p1->next=news; news->pre=p1; news->next=p; p->pre=news; return head; } } } int main() { nodes head; head=creatnew(); head=creat_list(head); put(head); Delete(head); put(head); //insert(head); //put(head); return 0; }
相关文章推荐
- 双向循环链表的建立,插入,删除(带头节点)
- java建立双向链表,插入结点,删除节点
- c++实现双向链表的建立,插入,删除,合并,打印
- 双向链表的初始化,建立,添加节点和删除节点(注意插入和删除要分三种情况)
- 双向链表建立、插入和删除
- 双向循环链表的建立,插入,删除(不带头结点)
- 双向链表的建立插入删除
- C语言习题 链表建立,插入,删除,输出
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作
- 数据结构---单链表(建立,节点删除,节点插入)
- 双向链表插入删除
- 数据结构 P36-37 算法实现 双向循环链表的插入与删除
- 链表的建立、插入和删除
- 单链表的基本操作:建立,求长度,输出,排序,插入,删除,逆置
- 链表的建立、插入和删除
- 建立一个带附加头结点的单链表.实现测长/打印/删除结点/插入结点/逆置/查找中间节点/查找倒数第k个节点/判断是否有环
- C语言链表的建立、插入和删除
- 链表:检测是否有环,是否相交,删除节点,插入节点,打印节点,建立节点,节点排序,节点逆序,