#include <stdio.h>
#include <malloc.h>
struct node
{
int inum;
struct node *pre;
struct node *next;
};
struct node *create(int num)
{
struct node *head;
struct node *tail,*p;
int i;
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
head->pre = NULL;
tail = head;
for(i = 1;i <= num;i++)
{
p = (struct node*)malloc(sizeof(struct node));
scanf("%d",&p->inum);
p->next = NULL;
p->pre = tail;
tail->next = p;
tail = p;
}
return (head);
}
void search(struct node *head)
{
struct node *p;
int NUM;
p = head->next;
scanf("%d",&NUM);
while(p != NULL)
{
if(p->inum == NUM)
{
if(p->pre != head &&p->next == NULL)
{
printf("%d\n",p->pre->inum);
break;
}
else if(p->pre == head &&p->next != NULL)
{
printf("%d\n",p->next->inum);
break;
}
else
{
printf("%d %d\n",p->pre->inum,p->next->inum);
}
break;
}else
p = p->next;
}
}
int main()
{
struct node *Head;
int inum1,inum2;
int i;
scanf("%d %d",&inum1,&inum2);
Head = create(inum1);
for(i = 1;i <= inum2;i++)
{
search(Head);
}
return 0;
}
sdut.acm2012级《程序设计基础Ⅱ)》_链表 双向链表
最新推荐文章于 2022-03-19 16:11:07 发布