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

来源:互联网 发布:多线程查询数据库 编辑:程序博客网 时间:2024/06/04 00:58
#include<bits/stdc++.h>  using namespace std;  typedef struct node  {      int data;      struct node *before,*next;  }LNode,*LinkList;  LinkList CreatList(int n)  {      LNode *head,*tail,*p;      head=new LNode;      head->next=NULL;      head->before=NULL;      tail=head;      for(int i=1;i<=n;i++)      {          p=new LNode ;          cin>>p->data;          p->next=NULL;          p->before=NULL;          tail->next=p;          p->before=tail;          tail=p;      }      return head;  }  void SearchLNode(LinkList head,int m)  {      LNode *p;      int key;      for(int i=1;i<=m;i++)      {          cin>>key;          p=head->next;          while(p)          {              if(p->data==key)              {                  if(p->next==NULL)                  {                      cout<<p->before->data<<endl;                      break;                  }                  else if(p==head->next)                  {                      cout<<p->next->data<<endl;                  }                  else                  {                      cout<<p->before->data<<" "<<p->next->data<<endl;                      break;                  }              }              p=p->next;          }        }    }  int main ()  {      int n,m;      cin>>n>>m;      LinkList head;      head=CreatList(n);      SearchLNode(head,m);      return 0;  }  /***************************************************User name: TJRAC6015203228魏杰Result: AcceptedTake time: 0msTake Memory: 160KBSubmit time: 2016-11-02 12:27:05****************************************************/

0 0
原创粉丝点击