01 | #include <iostream> |
02 | using namespace std; |
03 | class linklist { |
04 | private: |
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; |
13 | public: |
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 | }; |
38 | int 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 | } |
本文介绍了一个简单的双向链表实现及其操作示例,包括添加元素及特定元素位置的显示等基本功能。
811

被折叠的 条评论
为什么被折叠?



