#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next,*front;
};
int main()
{
int n,m,i;
scanf("%d %d",&n,&m);
struct node *p,*head,*tail;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
head->front=NULL; //建立两个工作指针,一个指向前,一个指向后;
tail=head;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=tail->next;//把p插到tail后面;
p->front=tail; //思考:为什么把这一行注释掉不行,既然下面说了tail后面是p,那么自然地p前面不就是tail吗?还非得说明。
tail->next=p;
tail=p;
}
while(m--)//执行m次操作
{
int t;
scanf("%d",&t);
for(p=head->next;p;p=p->next)
{
if(p->data==t) //先把他找出来,找到相应的数据就break,再去查看他所在位置;
{
break;
}
}
if(p->front!=head&&p->next!=NULL)//这个时候分情况讨论他的位置,不同位置输出不同;
{
printf("%d %d\n",p->front->data,p->next->data);
}
else if(p->front==head&&p->next!=NULL)
{
printf("%d\n",p->next->data);
}
else if(p->front!=head&&p->next==NULL)
{
printf("%d\n",p->front->data);
}
else break;
}
return 0;
}
SDUT OJ 数据结构实验之链表九:双向链表
最新推荐文章于 2022-08-04 15:38:28 发布