2054=数据结构实验之链表九:双向链表

 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 }

猜你喜欢

转载自www.cnblogs.com/Angfe/p/10485243.html