当前位置: 首页 > news >正文

算法竞赛阶段二-数据结构(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;

http://www.dtcms.com/a/300731.html

相关文章:

  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘ipywidgets’问题
  • 时间长了忘记jupyter的环境是哪个了
  • 07.4-使用 use 关键字引入路径
  • 基于分组规则的Excel数据分组优化系统设计与实现
  • 前端基础班学习路线
  • KNN算法实现图片的识别
  • Python堆栈实现:从基础到高并发系统的核心技术
  • Springboot+Layui英语单词学习系统的设计与实现
  • Python爬虫实战:研究netaddr库相关技术构建IP地址信息采集分析系统
  • Ubuntu服务器安装与运维手册——操作纯享版
  • uinput
  • 安卓打包遇到问题
  • RTSP|RTMP播放器 in Unity:开源不够用?从工程视角重新定义播放器选型
  • 特殊成员函数的生成规则:Effective Modern C++条款17解析
  • 商汤发布具身智能平台,让机器人像人一样和现实世界交互
  • 力扣 hot100 Day57
  • 定点数的表示
  • 批量提取Word中的图片,保存指定文件夹!源码分享
  • 电子电气架构 --- 软件bug的管理模式
  • ADB Shell 命令
  • 配置Dockerhub镜像源使用教程
  • Java生态下的AI开发利器:LangChain4j与Spring AI深度对比与实战
  • Tensorflow实现手写数字识别
  • 使用Python绘制动态樱花
  • 记录一次薛定谔bug
  • 2116. 判断一个括号字符串是否有效
  • GitHub 趋势日报 (2025年07月26日)
  • 零基础学习性能测试第五章:JVM性能分析与调优-多线程机制与运行原理
  • Rust赋能智能土木工程革新
  • LeetCode第350题_两个数组的交集II