算法竞赛阶段二-数据结构(36)数据结构双向链表模拟实现

//#include<bits/stdc++.h>

#include<iostream>

using namespace std;

const int N=1e5+10;

//定义

int e[N],pre[N],ne[N],h,id;

int mp[N];

//头插

// 兵 y

// x

void push_front (int x)

{

id++;

e[id]=x;

mp[x]=id;

pre[id]=h;

ne[id]=ne[h];

//先修改新节点

// pre[ne[id]]=id; ne【id】就是ne【h】看上边,刚赋的值

pre[ne[h]]=id;

// pre[id]=h;

ne[h]=id; //最后改这个

}

// 遍历打印,无视pre即可

void print()

{

for(int i=ne[h];i;i=ne[i])

{

cout<<e[i]<<" ";

}cout<<endl;

}

// 按值查找,mp数组优化·

int find(int x)

{

return (mp[x]);

}

// 任意位置(存储位置,下标)之后插入

// p

// 1 3

// x

void insert(int p,int x)

{

id++;

e[id]=x;

mp[x]=id;

pre[id]=p;

ne[id]=ne[p];

// pre[id]=p;

pre[ne[p]]=id;

ne[p]=id;

}

//任意位置(存储位置,下标)之前插入

// p

// 1 2

// x

void insret_front(int p,int x)

{

id++;

e[id]=x;

mp[x]=id;

ne[id]=p;

pre[id]=pre[p];

ne[pre[p]]=id;

pre[p]=id;

}

//删除任意位置元素

// p

// 1 2

// x

void erase(int p)

{

// mp[p]=0; 这个不对

mp[e[p]]=0;

ne[pre[p]]=ne[p];

pre[ne[p]]=pre[p];

}

int main()

{

for(int i=0;i<6;i++)

{

push_front(i);

}

print();

cout<< find(0)<<endl;

cout<< find(2)<<endl;

insert(1,33);

print();

insert(7,88);

print();

cout<< find(33)<<endl;

cout<< find(88)<<endl;

insret_front(1,66);

print();

insret_front(3,33);

print();

insret_front(8,100);

print();

insret_front(9,666);

print();

erase(1);

print();

cout<< find(100)<<endl;

erase(11);

print();

erase(7);

print();

erase(find(666));

print();

return 0;

}

相关推荐
buyutang_3 分钟前
C/C++ Linux系统编程:线程控制详解,从线程创建到线程终止
linux·c语言·c++·学习
jiaway9 分钟前
【C语言】第一课 环境配置
c语言·开发语言
Qiang_san29 分钟前
GNU Make | C/C++项目自动构建入门
c语言·c++·gnu
黑色的山岗在沉睡32 分钟前
LeetCode 189. 轮转数组
java·算法·leetcode
墨染点香32 分钟前
LeetCode 刷题【65. 有效数字】
算法·leetcode·职场和发展
小红帽2.01 小时前
从零构建一款开源在线客服系统:我的Go语言实战之旅
开发语言·golang·开源
slim~1 小时前
Java基础第9天总结(可变参数、Collections、斗地主)
java·开发语言
源代码•宸1 小时前
Leetcode—2749. 得到整数零需要执行的最少操作数【中等】(__builtin_popcountl)
c++·经验分享·算法·leetcode·位运算
用户4822137167751 小时前
深度学习——AlexNet网络结构
算法