您的位置:首页 > 其它

双向链表的简单操作

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  链表