《C语言 双向链表的建立 删除 插入.docx》由会员分享,可在线阅读,更多相关《C语言 双向链表的建立 删除 插入.docx(4页珍藏版)》请在三一办公上搜索。
1、C语言 双向链表的建立 删除 插入双向链表建立struct dnodeint data;struct dnode *prv;struct dnode *next;struct dnode *creatlist(int a)struct dnode *head=NULL,*p=NULL;head=(struct dnode *)malloc(sizeof(struct dnode);if(!head)perror(head malloc);head->prv=head->next=NULL; while(a)p=(struct dnode *)malloc(sizeof(struct d
2、node);if(!p)perror(p malloc);p->data=a-;p->next=head->next;head->next=p;p->prv=head;if(p->next!=NULL)p->next->prv=p;p=NULL;双向链表删除int dellist(struct list *head,int key)struct list *p,*w;if(head->next=NULL)return 1;/链表为空p=head;while(p->next!=NULL)/查找key值所在节点。if(p->next->data=ke
3、y)break;/命中p=p->next;if(p->prv)p->prv->next=p->next;if(p->next)p->next->prv=p->prv;free(p);双向链表插入int insert(struct dnode *head,int key,int a)struct dnode *p,*w=NULL;w=(struct dnode *)malloc(sizeof(struct dnode);if(!w)perrpr(w malloc);w->data=a;p=head->next;while(p&(p->data!=key)p=p->next;if(!p)return 1;p->prv->next=w;w->prv=p->prv;w->next=p;p->prv=w;return 1;版权:作孽