首页 技术 正文
技术 2022年11月16日
0 收藏 621 点赞 3,897 浏览 2040 个字

不更改链表结构,只是添加节点,没有删除节点。通过记录和更改标记来模拟题意的插入和删除,复制

指针模拟链表:

预开指针,存在M[]中,可以提高效率

#include<functional>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#include<map>
#include<set>
#include <stack>
#define REP(i, n) for(int i=0; i<n; i++)
#define PB push_back
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;const int maxn = 500005;struct point{
int val;
point *pre;
}M[maxn * 2];
int tot;
struct node{
point *a, *b;
}c[maxn];void add(point* &pre, int y)
{
point *u = &M[tot++];
u->val = y;
u->pre = pre;
pre = u;
}
void del(point* &last)
{
last = last->pre;
}
int t, m, n;int main()
{
char op[12];
tot = 1;
n = 1;
int x, y;
scanf("%d%d", &t, &m); while (t--)
{
scanf("%s", op);
scanf("%d", &x);
if (op[0] == 'l')
{
scanf("%d", &y); add(c[x].a, y);
c[x].b = NULL;
}
else if (op[0] == 'r' && op[1] == 'o')
{
if (c[x].a)
{
add(c[x].b, c[x].a->val);
del(c[x].a);
}
}
else if (op[0] == 'r' && op[1] == 'e')
{
if (c[x].b)
{
add(c[x].a, c[x].b->val);
del(c[x].b);
}
}
else if (op[0] == 'c' && op[1] == 'l')
{
c[++n] = c[x];
}
else
{
if (!c[x].a) printf("basic\n");
else printf("%d\n", c[x].a->val);
}
}
}

数组模拟链表:

#include<functional>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#include<map>
#include<set>
#include <stack>
#define REP(i, n) for(int i=0; i<n; i++)
#define PB push_back
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;const int maxn = 500005;struct point{
int val;
int pre;
}P[maxn * 2];struct node{
int a, b;
node(){}
node(int a, int b) : a(a), b(b){}
}c[maxn];
int tot;int t, m, n;void add(int u, int &pre, int y)
{
P[tot].pre = pre;
P[tot].val = y;
pre = tot++;
}void del(int &last)
{
last = P[last].pre;
}int main()
{
char op[12];
n = 1;
tot = 1;
int x, y;
scanf("%d%d", &t, &m); while (t--)
{
scanf("%s", op);
scanf("%d", &x);
if (op[0] == 'l')
{
scanf("%d", &y); add(x, c[x].a, y);
c[x].b = 0;
}
else if (op[0] == 'r' && op[1] == 'o')
{
if (c[x].a)
{
add(x, c[x].b, P[c[x].a].val);
del(c[x].a);
}
}
else if (op[0] == 'r' && op[1] == 'e')
{
if (c[x].b)
{
add(x, c[x].a, P[c[x].b].val);
del(c[x].b);
}
}
else if (op[0] == 'c' && op[1] == 'l')
{
c[++n] = node(c[x].a, c[x].b);
}
else
{
if (!c[x].a) printf("basic\n");
else printf("%d\n", P[c[x].a].val);
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,033
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862