2054=數據結構實驗之鏈表九:雙向鏈表
阿新 • • 發佈:2019-03-06
div ret \n 下一個 main stdio.h 上一個 int 單向
1 #include <stdio.h> 2 #include <stdlib.h> 3 struct node 4 { 5 int data; 6 struct node*next,*last; 7 }; 8 int main() 9 { 10 int m,n,x,i,a; 11 struct node*head,*p,*end; 12 head=(struct node*)malloc(sizeof(struct node)); 13 head->next=NULL;14 end=head; 15 scanf("%d %d",&n,&m); 16 for(i=0; i<n; i++) 17 { 18 p=(struct node*)malloc(sizeof(struct node)); 19 scanf("%d",&p->data); 20 p->next=NULL; 21 end->next=p; 22 p->last=end;//雙向鏈表其實和單向鏈表沒啥區別,就是在下一個加了一個(上一個)。23 end=p; 24 } 25 for(i=0; i<m; i++) 26 { 27 scanf("%d",&a); 28 for(p=head->next; p; p=p->next)//這裏是head不是NULL; 29 { 30 if(p->data==a) 31 { 32 if(p->last!=head&&p->next!=NULL)printf("%d %d\n",p->last->data,p->next->data); 33 else if(p->last!=head)printf("%d\n",p->last->data); 34 else if(p->next!=NULL)printf("%d\n",p->next->data); 35 } 36 } 37 } 38 return 0; 39 }
2054=數據結構實驗之鏈表九:雙向鏈表