64,687
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
struct node
{
int data;
struct node* prve;
struct node* next;
};
node *Br(int n)
{
struct node *h,*p,*q;
h=new struct node;
p=new struct node;
h->next=NULL;
h->prve=NULL;
p=h;
for(int i=0;i<n;i++)//构建链表
{
int x;
cin>>x;
q=new struct node ;
q->data=x;
q->next=NULL;
if(h->next==NULL)
{
q->prve=h;
}
else{
q->prve=p;
}
p->next=q;
p=q;
}
return h;
}
int main()
{
int n,m;
cin>>n>>m;
struct node *h,*p,*q;
h=Br(n);
for(int i=0;i<m;i++)
{
int t;
cin>>t;
p=h;
while(p->data!=t)//把p停到要找的那个结点处
{
p=p->next;
}
int lll=0;
if(p->prve!=h)//输出前驱
{
cout<<p->prve->data;
lll=1;
}
if(p->next!=NULL){//输出后继
if(lll)cout<<" "<<p->next->data;
else cout<<p->next->data;
}
if(i!=m-1)cout<<endl;
}
return 0;
}