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

来源:互联网 发布:激光亚克力切割机软件 编辑:程序博客网 时间:2024/06/11 07:07
01#include <iostream>
02using namespace std;
03class linklist {
04private:
05    classlinknode {
06    public:
07        intdata;
08        linknode *pre, *next;
09        linknode():pre(NULL),next(NULL) {}
10        linknode(intd):data(d),pre(NULL),next(NULL) {}
11    };
12    linknode *head, *tail;
13public:
14    linklist() {
15        head=newlinknode();
16        tail=head;
17    }
18    voidadd(int data) {
19        linknode *temp=newlinknode(data);
20  
21        tail->next=temp;
22        temp->pre=tail;
23        tail=temp;
24    }
25    voiddisplay(int x) {
26        linknode *p=head->next;
27        while(x!=p->data&&p) {
28            p=p->next;
29        }
30        if(p) {
31            if(p==head->next) cout<<p->next->data<<endl;
32            elseif(p==tail) cout<<p->pre->data<<endl;
33            else
34            cout<<p->pre->data<<' '<<p->next->data<<endl;
35        }
36    }
37};
38int main() {
39    intn, m;
40    cin>>n>>m;
41    linklist l;
42    while(n--) {
43        intx;
44        cin>>x;
45        l.add(x);
46    }
47    while(m--) {
48        intx;
49        cin>>x;
50        l.display(x);
51    }
52    return0;
53
0 0