双向链表操作大全C语言实现
来源:互联网 发布:如何建立网络链接 编辑:程序博客网 时间:2024/04/30 19:27
双向链表的操作和单链表很像,主要包括创建,删除,插入;只要注意下指针的操作即可,废话不多说,直接上代码:
typedef struct dnode{ int num; struct dnode *pre; struct dnode *next;}Dnode,*pDnode;pDnode Create() //用户输入创建,-1表示结束{ pDnode head=(pDnode)malloc(sizeof(Dnode)); pDnode p=head; int a; printf("please input :\n"); scanf("%d",&a); while(a!=-1) { pDnode q=(pDnode)malloc(sizeof(Dnode)); q->num=a; p->next=q; q->pre=p; p=q; scanf("%d",&a); } p->next=NULL; head=head->next; head->pre=NULL; return head;}pDnode CreateFromArray(int a[],int len) //根据数组创建{ pDnode head=(pDnode)malloc(sizeof(Dnode)); pDnode p=head; int i=0; while(i<len) { pDnode q=(pDnode)malloc(sizeof(Dnode)); q->num=a[i]; p->next=q; q->pre=p; p=q; i++; } p->next=NULL; head=head->next; head->pre=NULL; return head;}pDnode del(pDnode head, int key) //删除{ pDnode q1=head; pDnode q2=head; if(head==NULL) return NULL; if(head->num==key) { head=head->next; head->pre=NULL; free(q1); return head; } while(q1->num!=key&&q1->next!=NULL) { q2=q1; q1=q1->next; } if(q1->num==key&&q1->next==NULL) { q2->next=NULL; free(q1); } else if(q1->num==key&&q1->next!=NULL) { q2->next=q1->next; q1->next->pre=q2; free(q1); } else { printf("not found\n"); } return head;}pDnode insert(pDnode head,int key) // 插入{ pDnode s=(pDnode)malloc(sizeof(Dnode)); s->num=key; pDnode p=head; pDnode q=head; while(p->num<key&&p->next!=NULL) { p=p->next; } if(p==head) { s->next=head; head->pre=s; head=s; head->pre=NULL; } else if(p->next==NULL&&p->num<key) { p->next=s; s->pre=p; s->next=NULL; } else { q=p->next; p->next=s; s->next=q; s->pre=p; q->pre=s; } return head;}
void print_from_head(pDnode head) //打印{ while(head->next!=NULL) { printf("%d",head->num); head=head->next; } printf("%d",head->num); printf("\n");}void print_from_tail(pDnode head){ while(head->next!=NULL) head=head->next; while(head->pre!=NULL) { printf("%d",head->num); head=head->pre; } printf("%d",head->num); printf("\n");}
- 双向链表操作大全C语言实现
- 双向链表的实现与操作(C语言实现)
- C语言实现双向链表的基本操作
- C语言实现双向链表的基本操作
- 双向链表的操作实现(c语言)
- 双向链表的相关操作--C语言实现
- 数据结构之---c语言实现双向链表操作
- c语言实现双向链表的基本操作
- C语言实现双向链表删除节点、插入节点、双向输出等操作
- C语言实现双向链表删除、插入、双向输出
- 双向链表代码实现-C语言
- 双向链表C语言实现
- C语言实现双向链表[上]
- C语言实现双向链表
- c语言双向链表的实现
- C语言实现双向循环链表
- C语言实现双向链表
- 双向链表的C语言实现
- MySQL出错及解决方法(网上收集)
- Android-NDK入门案例及基础问题解决(一)
- UML调试linux2.6.34.14中的sys_socketcall
- Problem running post-install step
- Spring MVC3.0.5搭建全程
- 双向链表操作大全C语言实现
- 分享一个GSM短信猫接口程序
- MySQL性能优化基础
- VS2008 Debug与Release的本质区别
- 软考信息系统项目管理师考试心得-备考
- mysql 存储过程
- d叉堆
- MySQL日期处理
- shell中引号的用法