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

本文介绍了一个简单的双向链表实现及其操作示例,包括添加元素及特定元素位置的显示等基本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值