AcWing 827:双链表

【题目描述】
实现一个双链表,双链表初始为空,支持 5 种操作:

1.在最左侧插入一个数; 
2.在最右侧插入一个数; 
3.将第 k 个插入的数删除; 
4.在第 k 个插入的数左侧插入一个数; 
5.在第 k 个插入的数右侧插入一个数。
现在要对该链表进行 M 次操作,进行完所有操作后,从左到右输出整个链表。
注意:题目中第 k 个插入的数并不是指当前链表的第 k 个数。例如操作过程中一共插入了 n 个数,则按照插入的时间顺序,这 n 个数依次为:第 1 个插入的数,第 2 个插入的数,…第 n 个插入的数。

【输入格式】
第一行包含整数 M,表示操作次数。
接下来 M 行,每行包含一个操作命令,操作命令可能为以下几种:

1.L x:表示在链表的最左端插入数 x。
2.R x:表示在链表的最右端插入数 x。
3.D k:表示将第 k 个插入的数删除。
4.IL k x:表示在第 k 个插入的数左侧插入一个数。
5.IR k x:表示在第 k 个插入的数右侧插入一个数。
【输出格式】
共一行,将整个链表从左到右输出。

【数据范围】
1≤M≤100000
所有操作保证合法。

【输入样例】
10
R 7
D 1
L 3
IL 2 10
D 3
IL 2 7
L 8
R 9
IL 4 7
IR 2 2

【输出样例】
8 7 7 3 2 9

主要用STL标准库中的list函数详见:https://cplusplus.com/reference/list/list/

代码

#include <bits/stdc++.h>
using namespace std;
 
list<int> lst;
vector<list<int>::iterator> v;
int T,k,x;
string op;
 
int main() {
    cin>>T;
    while(T--) {
        cin>>op;
        if(op=="L") {
            cin>>x;
            lst.push_front(x);
            v.push_back(lst.begin());
        } else if(op=="R") {
            cin>>x;
            lst.push_back(x);
            v.push_back(--lst.end());
        } else if(op=="D") {
            cin>>k;
            lst.erase(v[k-1]);
        } else if(op=="IL") {
            cin>>k>>x;
            auto it=v[k-1];
            v.push_back(lst.insert(it,x));
        } else if(op=="IR") {
            cin>>k>>x;
            auto it=v[k-1];
            v.push_back(lst.insert(next(it),x));
        }
    }
 
    for(auto x:lst) cout<<x<<" ";
    return 0;
}
 
/*
in:
10
R 7
D 1
L 3
IL 2 10
D 3
IL 2 7
L 8
R 9
IL 4 7
IR 2 2
out:
8 7 7 3 2 9
*/

### 关于双链表的实现 双链表是一种重要的数据结构,它由节点组成,每个节点包含三个部分:前驱指针、数据域和后继指针。这种结构允许双向遍历,因此相较于单链表更加灵活。 以下是双链表的核心操作实现: #### 节点定义 ```java class Node { int value; Node prev, next; public Node(int value) { this.value = value; this.prev = null; this.next = null; } } ``` #### 插入操作 在双链表中插入新节点的操作需要调整前后节点的关系。 ```java public void insert(Node head, int position, int newValue) { Node newNode = new Node(newValue); if (position == 0) { // 头插法 newNode.next = head; if (head != null) { head.prev = newNode; } head = newNode; } else { Node current = head; for (int i = 0; i < position - 1 && current != null; i++) { current = current.next; } if (current != null) { newNode.next = current.next; newNode.prev = current; if (current.next != null) { current.next.prev = newNode; } current.next = newNode; } } } ``` #### 删除操作 删除指定位置的节点时需要注意更新其前后节点的连接关系。 ```java public void delete(Node head, int position) { if (position == 0) { // 删除头结点 if (head != null) { head = head.next; if (head != null) { head.prev = null; } } } else { Node current = head; for (int i = 0; i < position && current != null; i++) { current = current.next; } if (current != null) { if (current.prev != null) { current.prev.next = current.next; } if (current.next != null) { current.next.prev = current.prev; } } } } ``` 以上代码展示了如何通过 Java 来实现双链表的基本功能[^1]。 --- ### AcWing 上的相关练习题 AcWing 提供了许多与双链表相关的经典题目,这些题目可以帮助巩固对双链表的理解并提升编程能力。以下是一些推荐的练习题: 1. **827. 双链表** 题目描述通常涉及构建一个支持增删查改操作的双链表,并测试其实现是否正确[^3]。 2. **模拟队列/栈** 使用双链表作为底层存储结构来实现队列或栈的功能,考察对其基本操作的应用。 3. **动态数组扩展** 结合双链表的特点设计一种能够自动扩容的数据容器,类似于 `ArrayList` 的行为。 4. **区间合并问题** 利用双链表解决某些区间的交集、并集运算等问题,这类场景下双链表的优势尤为明显。 上述题目均可以在 AcWing 数据结构章节找到,建议按照难度逐步尝试完成[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值