双向链表的简单操作
2016-09-08 23:31
351 查看
双链表的结构体
双链表的创建
双链表的遍历
双链表的插入
双链表的删除
以下是所有的操作
typedef struct Linklist{ int data; struct Linklist *prior,*next; }node;
双链表的创建
node *create(){ node *head,*p1,*p2; head=(node*)malloc(sizeof(node)); head->next=NULL; p1=(node*)malloc(sizeof(node)); printf("please input data:"); scanf("%d",&p1->data); while(p1->data){ len++; if(len==1){ head->next=p1; p1->prior=head; p2=p1; } else{ p2->next=p1; p1->prior=p2; p2=p1; } p1=(node*)malloc(sizeof(node)); printf("please input data:"); scanf("%d",&p1->data); } free(p1); p2->next=NULL; return head; }
双链表的遍历
void disp(node *head){ node *p=head->next; node *q; printf("len=%d\n",len); while(p){ printf("%d ",p->data); printf("%d ",p->prior->data); //上一个节点的值,检查prior是否连接正常 printf("\n"); p=p->next; } }
双链表的插入
node *insert(node *head,node *newnode,int pos){ //插入的节点在首部 if(pos>len+1){ printf("pos error!\n"); exit(1); } if(pos==0){ printf("please input data:"); scanf("%d",&newnode->data); newnode->next=head->next; head->next->prior=newnode; head->next=newnode; newnode->prior=head; } //插入的节点在中间 else if((pos>0)&&(pos<len)){ printf("please input data:"); scanf("%d",&newnode->data); node *p=head->next; int cnt=1; while(p){ if(pos==cnt){ node *q=p->next; p->next=newnode; newnode->prior=p; newnode->next=q; q->prior=newnode; break; } p=p->next; cnt++; } } //插入节点在尾部 else if(pos==len){ node *p=head->next; while(p->next!=NULL){ p=p->next; } printf("please input data:"); scanf("%d",&newnode->data); p->next=newnode; newnode->prior=p; } len++; return head; }
双链表的删除
node *Delete(node *head,int pos){ if(pos > len){ printf("pos error!\n"); exit(1); } //int cnt=1; if(pos==1){ node *q=head->next->next; head->next=q; q->prior=head; } else if((pos>1)&&(pos<len)){ int cnt=2; node *p=head->next; while(p){ if(pos==cnt){ node *q= p->next->next; p->next=q; q->prior=p; break; } } } else if(pos==len){ node *p=head->next; while(p->next->next){ p=p->next; } p->next=NULL; } len--; return head; }
以下是所有的操作
#include<stdio.h>
#include<stdlib.h>
int len=0;
//双向链表的定义
typedef struct Linklist{ int data; struct Linklist *prior,*next; }node;
//双向链表的创建
node *create(){ node *head,*p1,*p2; head=(node*)malloc(sizeof(node)); head->next=NULL; p1=(node*)malloc(sizeof(node)); printf("please input data:"); scanf("%d",&p1->data); while(p1->data){ len++; if(len==1){ head->next=p1; p1->prior=head; p2=p1; } else{ p2->next=p1; p1->prior=p2; p2=p1; } p1=(node*)malloc(sizeof(node)); printf("please input data:"); scanf("%d",&p1->data); } free(p1); p2->next=NULL; return head; }
void disp(node *head){ node *p=head->next; node *q; printf("len=%d\n",len); while(p){ printf("%d ",p->data); printf("%d ",p->prior->data); //上一个节点的值,检查prior是否连接正常 printf("\n"); p=p->next; } }
//节点的插入
node *insert(node *head,node *newnode,int pos){ //插入的节点在首部 if(pos>len+1){ printf("pos error!\n"); exit(1); } if(pos==0){ printf("please input data:"); scanf("%d",&newnode->data); newnode->next=head->next; head->next->prior=newnode; head->next=newnode; newnode->prior=head; } //插入的节点在中间 else if((pos>0)&&(pos<len)){ printf("please input data:"); scanf("%d",&newnode->data); node *p=head->next; int cnt=1; while(p){ if(pos==cnt){ node *q=p->next; p->next=newnode; newnode->prior=p; newnode->next=q; q->prior=newnode; break; } p=p->next; cnt++; } } //插入节点在尾部 else if(pos==len){ node *p=head->next; while(p->next!=NULL){ p=p->next; } printf("please input data:"); scanf("%d",&newnode->data); p->next=newnode; newnode->prior=p; } len++; return head; }
//节点的删除
node *Delete(node *head,int pos){ if(pos > len){ printf("pos error!\n"); exit(1); } //int cnt=1; if(pos==1){ node *q=head->next->next; head->next=q; q->prior=head; } else if((pos>1)&&(pos<len)){ int cnt=2; node *p=head->next; while(p){ if(pos==cnt){ node *q= p->next->next; p->next=q; q->prior=p; break; } } } else if(pos==len){ node *p=head->next; while(p->next->next){ p=p->next; } p->next=NULL; } len--; return head; }
int main(){
node *head=create();
disp(head);
node *newnode=(node*)malloc(sizeof(node));
//Delete(head,1);
insert(head,newnode,0);
disp(head);
return 0;
}
相关文章推荐
- (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作
- c语言 之 双向链表 简单操作
- (C++版)链表(三)——实现双向链表的创建、插入、删除等简单操作
- (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作
- C++ 双向链表的简单操作
- (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作
- (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作
- 双向链表的简单操作
- 双向链表简单操作
- 双向链表的简单操作
- (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作
- 双向链表的简单操作
- 详谈双向链表的实现与简单操作
- (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作
- (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作
- BNUOJ 26182 -----------Army Buddies 用结构体 仿双向链表的简单操作
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作
- 数据结构:链表的简单操作
- 双向链表的操作 模板的特化