Acwing 827 双链表

本文介绍了一种使用双向链表进行元素插入、删除等操作的算法实现,通过维护左右指针,实现了在指定位置插入元素、删除指定元素等功能,最后遍历链表输出所有元素。

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

开始把0设为head,1设为tail,所以编号为k的节点为第k+1个插入的数;

#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
const int N=1e5+10;
int l[N],r[N],idx,e[N];//l,r分别为左右指针
void init()
{
    r[0]=1;
    l[1]=0;
    idx=2;
}
//k右边插入x
void add(int k,int x)
{
    e[idx]=x;
    r[idx]=r[k];
    l[idx]=k;
    l[r[k]]=idx;
    r[k]=idx;
    idx++;
}
void remove(int k)
{
    r[l[k]]=r[k];
    l[r[k]]=l[k];
}
int main()
{
    int n;
    cin>>n;
    init();
    while(n--)
    {
        string op;
        int x;
        int k;
        cin>>op;
        if(op=="L")
        {
            cin>>x;
            add(0,x);
        }
        else if(op=="R")
        {
            cin>>x;
            add(l[1],x);
        }
        else if(op=="D")
        {
            cin>>k;
            remove(k+1);
        }
        else if(op=="IL")//在第k个数左边添加一个数
        {
            cin>>k>>x;
            add(l[k+1],x);//即在第k个数左指针指向的数的右边添加
        }
        else 
        {
            cin>>k>>x;
            add(k+1,x);
        }
    }
    for(int i=r[0];i!=1;i=r[i])
    {
        cout<<e[i]<<" ";
    }
    return 0;
}
### 关于双链表的实现 双链表是一种重要的数据结构,它由节点组成,每个节点包含三个部分:前驱指针、数据域和后继指针。这种结构允许双向遍历,因此相较于单链表更加灵活。 以下是双链表的核心操作实现: #### 节点定义 ```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、付费专栏及课程。

余额充值