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

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

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

学会了单向链表,我们又多了一种解决问题的能力,单链表利用一个指针就能在内存中找到下一个位置,这是一个不会轻易断裂的链。但单链表有一个弱点——不能回指。比如在链表中有两个节点A,B,他们的关系是B是A的后继,A指向了B,便能轻易经A找到B,但从B却不能找到A。一个简单的想法便能轻易解决这个问题——建立双向链表。在双向链表中,A有一个指针指向了节点B,同时,B又有一个指向A的指针。这样不仅能从链表头节点的位置遍历整个链表所有节点,也能从链表尾节点开始遍历所有节点。对于给定的一列数据,按照给定的顺序建立双向链表,按照关键字找到相应节点,输出此节点的前驱节点关键字及后继节点关键字。

Input

第一行两个正整数n(代表节点个数),m(代表要找的关键字的个数)。第二行是n个数(n个数没有重复),利用这n个数建立双向链表。接下来有m个关键字,每个占一行。

Output

对给定的每个关键字,输出此关键字前驱节点关键字和后继节点关键字。如果给定的关键字没有前驱或者后继,则不输出。
注意:每个给定关键字的输出占一行。
           一行输出的数据之间有一个空格,行首、行末无空格。

 

Example Input
10 31 2 3 4 5 6 7 8 9 0350
Example Output
2 44 69
Hint

Author

//code:

01 #include<bits/stdc++.h>
02 using namespace std;
03  
04 typedef struct node
05 {
06     int data;
07     struct node *before, *after;
08 }LNode, *LinkList;
09  
10 LinkList  CreatList(int n)
11 {
12     LNode  *head, *tail, *p;
13     head=new LNode;
14     head -> before = NULL;
15     head -> after =  NULL;
16     tail = head;
17     for(int  i = 0; i < n; i ++)
18     {
19         p=new node;
20         cin>>p->data;
21         p -> before = NULL;
22         p -> after = NULL;
23         tail -> after= p;
24         p -> before = tail;//错这了……
25         tail = p;
26     }
27     return head;
28 }
29 void SearchLNode(LinkList  head, int m)
30 {
31     LNode *p;
32     int keyword;
33     for(int  i = 0; i < m ;i ++)
34     {
35         cin>>keyword;
36         p=head->after;
37         while(p)
38         {
39             if(p -> data == keyword)
40             {
41                 if(p->after == NULL)
42                 {
43                     cout << p ->before -> data << endl;
44                     break;
45                 }
46                 else if(p==head->after)
47                 {
48                     cout << p->after->data<<endl;
49                     break;
50                 }
51                 else
52                 {
53                     cout <<p->before->data<<" "<<p->after->data<<endl;
54                     break;
55                 }
56              }
57              p = p -> after;
58          }
59     }
60 }
61 int main()
62 {
63     int n,m;
64     cin>>n>>m;
65     LinkList head;
66     head = CreatList(n);
67     SearchLNode(head, m);
68     return 0;
69 }

//code2:
01 #include<bits/stdc++.h>
02 using namespace std;
03 typedef struct node
04 {
05     int data;
06     struct node *on,*under;
07 }link_list;
08  
09 link_list *creat_list (int n)
10 {
11     link_list *head,*p,*tail;
12     head = new node;
13     head -> on = NULL;
14     head -> under = NULL;
15     tail = head;
16     for(int i=0;i<n;i++)
17     {
18         p=new node;
19         cin >> p -> data;
20         p -> on = NULL;
21         p -> under = NULL;
22         tail -> under = p;
23         p -> on = tail;
24         tail = p;
25     }
26     return head;
27 }
28  
29 void search_list ( link_list *head, int m)
30 {
31     int key_word ;
32     link_list *p;
33     for(int i=0;i<m;i++)
34     {
35         cin >> key_word;
36         p = head -> under;
37         while(p)
38         {
39             if(key_word == p->data)
40             {
41                 if(p->under==NULL)
42                 {
43                     cout << p->on -> data <<endl;
44                     break;
45                 }
46                 else if(head -> under == p)
47                 {
48                     cout << p -> under -> data << endl;
49                     break;
50                 }
51                 else{
52                     cout << p->on->data << " " <<p->under->data<<endl;
53                     break;
54                 }
55             }
56             p = p -> under;
57         }
58     }
59 }
60  
61 int main()
62 {
63     link_list *head;
64     int m,n;
65     cin >>n>>m;
66     head = creat_list(n);
67     search_list(head,m);
68     return 0;
69 }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鹿海园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值