AcWing 826. 单链表
#include<iostream>
using namespace std;
const int N = 100010;
int head,idx,e[N],ne[N];
void init()
{
head = -1;
idx = 0;
}
void del(int k)
{
ne[k] = ne[ne[k]];
}
void add_head(int x)
{
e[idx] = x;
ne[idx] = head;
head = idx++;
}
void add(int k,int x)
{
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx++;
}
int main()
{
int n;cin >> n;
init();
while(n--)
{
int x,k;
char ch;cin >> ch;
if(ch == 'H')
{
cin >> x;
add_head(x);
}
else if(ch == 'D')
{
cin >> k;
if(!k) head = ne[head];
del(k - 1);
}
else if(ch == 'I')
{
cin >> k >> x;
add(k - 1,x);
}
}
for(int i = head;i != -1;i = ne[i]) cout << e[i] << ' ';
cout << endl;
return 0;
}
AcWing 827. 双链表
#include<iostream>
using namespace std;
const int N = 100010;
int n,x,k,e[N],l[N],r[N],idx;
void init()
{
r[0] = 1,l[1] = 0,idx = 2;
}
void add(int k,int x)
{
e[idx] = x;
r[idx] = r[k];
l[idx] = k;
l[r[k]] = idx;
r[k] = idx++;
}
void del(int k)
{
l[r[k]] = l[k];
r[l[k]] = r[k];
}
int main()
{
cin >> n;
init();
while(n -- )
{
string a;cin >> a;
if(a == "L")
{
cin >> x;
add(0,x);
}
else if(a == "R")
{
cin >> x;
add(l[1],x);
}
else if(a == "IL")
{
cin >> k >> x;
add(l[k + 1],x);
}
else if(a == "IR")
{
cin >> k >> x;
add(k + 1,x);
}
else
{
cin >> k;
del(k + 1);
}
}
for(int i = r[0];i != 1;i = r[i]) cout << e[i] << ' ';
cout << endl;
return 0;
}
AcWing 828. 模拟栈
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100010;
int x, m, stk[N], tt;
int main()
{
cin >> m;
while (m--)
{
string a; cin >> a;
if (a == "push")
{
cin >> x;
stk[++tt] = x;
}
else if (a == "pop")
{
tt--;
}
else if (a == "empty")
{
if (!tt) puts("YES");
else puts("NO");
}
else if (a == "query")
{
cout << stk[tt] << endl;
}
}
return 0;
}
|