AcWing 826. 单链表之听课小记
原题链接
简单
作者:
王尔德_4
,
2024-02-16 11:31:30
,
所有人可见
,
阅读 43
插入操作
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N=100010;
int e[N],ne[N],head,idx;
//单链表->邻接表->存储图
// ->树
//双链表->优化某些问题
// head 表示头结点的下标 3 5 7 9
// e[i] 表示节点i的值 head->o->o->o->o->o
// ne[i] 表示节点i的next指针是多少 0 1 2 3
// idx 存储当前已经用到了哪个点 e[0]=3,e[1]=5,e[2]=7
void init() ne[0]=1,ne[1]=2,ne[2]=3,ne[3]=4
{
head=-1;
idx=0;
}
表头插入
void add_to_head(int x)
{
e[idx]=x;
ne[idx]=head;//head没赋值前指向下一个节点,此行意思为将ne[idx]指向原来的head所指向的下标
head=idx;//更新head所指向的下标
idx++;
}
表中插入
void add(int k,int x)
{
e[idx]=x;
ne[idx]=ne[k];
ne[k]=idx;//ne[]就是指下标的
idx++;
}
单链表的删除操作
void remove(int k)//数组模拟链表叫静态链表
{
ne[k]=ne[ne[k]];//自身的叠加,要知道跳过就是删除,这个应该是顺序的删除,单链表不能查找,删除上一个点
}