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

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

#include<iostream>

using namespace std;

const int N=1e5+10;

//定义

int eN,preN,neN,h,id;

int mpN;

//头插

// 兵 y

// x

void push_front (int x)

{

id++;

eid=x;

mpx=id;

preid=h;

neid=neh;

//先修改新节点

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

prene\[h]=id;

// preid=h;

neh=id; //最后改这个

}

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

void print()

{

for(int i=neh;i;i=nei)

{

cout<<ei<<" ";

}cout<<endl;

}

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

int find(int x)

{

return (mpx);

}

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

// p

// 1 3

// x

void insert(int p,int x)

{

id++;

eid=x;

mpx=id;

preid=p;

neid=nep;

// preid=p;

prene\[p]=id;

nep=id;

}

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

// p

// 1 2

// x

void insret_front(int p,int x)

{

id++;

eid=x;

mpx=id;

neid=p;

preid=prep;

nepre\[p]=id;

prep=id;

}

//删除任意位置元素

// p

// 1 2

// x

void erase(int p)

{

// mpp=0; 这个不对

mpe\[p]=0;

nepre\[p]=nep;

prene\[p]=prep;

}

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;

}

相关推荐
yume_sibai1 分钟前
03-Rust 函数式编程特性(闭包 + Iterator + Option/Result + 链式调用)
开发语言·后端·rust
xxwxx__4 分钟前
深入理解 C++ 继承:从基础语法到底层原理全解析
c++
Felven6 分钟前
B. Deja Vu
数据结构·算法
键盘会跳舞7 分钟前
【C++多线程】多线程性能调优:伪共享、缓存行对齐与吞吐优化
c++·多线程·多线程调优
奈斯先生Vector11 分钟前
AIGC 视频生成实战:用 Kling Video 拆解文生视频、图生视频与完整工作流
开发语言·人工智能·windows·python·aigc·音视频
小玮看世界16 分钟前
[Python]螺旋遍历 vs 最短路径:方向控制类算法的“同源异流”
开发语言·python·算法
月华路21 分钟前
《模型不玄学》第14章 标签、损失与样本权重
人工智能·算法·机器学习
黄金龙PLUS23 分钟前
SPARKLE置换算法的优缺点
算法·网络安全·密码学·哈希算法·同态加密
不会就选b23 分钟前
算法日常・每日刷题--<贪心>3
数据结构·算法·leetcode
ysu_031424 分钟前
03-双链表与循环链表
c语言·数据结构·链表