目录
1、单链表
1.1 模板
// head存储链表头,e[]存储节点的值,ne[]存储节点的next指针,idx表示当前用到了哪个节点
int head, e[N], ne[N], idx;
// 初始化
void init()
{
head = -1;
idx = 0;
}
// 在链表头插入一个数a
void insert(int a)
{
e[idx] = a, ne[idx] = head, head = idx ++ ;
}
// 将头结点删除,需要保证头结点存在
void remove()
{
head = ne[head];
}
1.2 例题
题目来源:AcWing
题目链接:AcWing 826. 单链表
代码展示:
#include <iostream>
using namespace std;
const int N = 100010;
int head, e[N], ne[N], idx, m;
void init()
{
head = -1;
idx = 0;
}
void insert_to_head(int a)
{
e[idx] = a;
ne[idx] = head;
head = idx ++;
}
void insert_to_k(int k, int a)
{
e[idx] = a;
ne[idx] = ne[k];
ne[k] = idx ++;
}
void remove(int k)
{
ne[k] = ne[ne[k]];
}
int main()
{
init();
cin >> m;
while(m -- )
{
int k, x;
char op;
cin >> op;
if(op == 'H')
{
cin >> x;
insert_to_head(x);
}else if(op == 'I')
{
cin >> k >> x;
insert_to_k(k - 1, x);
}else
{
cin >> k;
if(!k) head = ne[head];
else remove(k - 1);
}
}
for(int i = head; i != -1; i = ne[i])
{
cout << e[i] << ' ';
}
return 0;
}
2、双链表
2.1 模板
// e[]表示节点的值,l[]表示节点的左指针,r[]表示节点的右指针,idx表示当前用到了哪个节点
int e[N], l[N], r[N], idx;
// 初始化
void init()
{
//0是左端点,1是右端点
r[0] = 1, l[1] = 0;
idx = 2;
}
// 在节点a的右边插入一个数x
void insert(int a, int x)
{
e[idx] = x;
l[idx] = a, r[idx] = r[a];
l[r[a]] = idx, r[a] = idx ++ ;
}
// 删除节点a
void remove(int a)
{
l[r[a]] = l[a];
r[l[a]] = r[a];
}
2.2 例题
题目来源:AcWing
题目链接:AcWing 827. 双链表
代码展示:
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int m, e[N], l[N], r[N], idx;
void init(){
//0为最左端点,1为最右端点
r[0] = 1, l[1] = 0;
idx = 2;
}
//向结点k的右侧插入一个数
void insert_k_right(int k, int x){
e[idx] = x;
l[idx] = k, r[idx] = r[k];
l[r[k]] = idx, r[k] = idx++;
}
//删除结点k后面的数
void remove(int k){
l[r[k]] = l[k];
r[l[k]] = r[k];
}
int main()
{
init();
cin >> m;
while(m --){
string op;
int k, x;
cin >> op;
if(op == "L"){
cin >> x;
insert_k_right(0, x);
}else if(op == "R"){
cin >> x;
insert_k_right(l[1], x);
}else if(op == "D"){
cin >> k;
remove(k + 1);
}else if(op == "IL"){
cin >> k >> x;
insert_k_right(l[k + 1], x);
}else{
cin >> k >> x;
insert_k_right(k + 1, x);
}
}
for(int i = r[0]; i != 1; i = r[i]){
cout << e[i] << " ";
}
return 0;
}
3、栈
3.1 模板
// tt表示栈顶
int stk[N], tt = 0;
// 向栈顶插入一个数
stk[ ++ tt] = x;
// 从栈顶弹出一个数
tt -- ;
// 栈顶的值
stk[tt];
// 判断栈是否为空,如果 tt > 0,则表示不为空
if (tt > 0)
{
}
3.2 例题
题目来源:AcWing
题目链接:AcWing 828. 模拟栈
代码展示:
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int stk[N], tt = 0, m;
void add(int x) {
stk[ ++ tt] = x;
}
int main()
{
cin >> m;
while(m --){
string op;
cin >> op;
if(op == "push"){
int x;
cin >> x;
add(x);
}else if(op == "pop"){
tt --;
}else if(op == "query"){
cout << stk[tt] << endl;
}else{
if(tt > 0) cout << "NO" << endl;
else cout << "YES" << endl;
}
}
return 0;
}
题目来源:AcWing
题目链接:AcWing 3302. 表达式求值
代码展示:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>
using namespace std;
stack<int> num;
stack<char> op;
void eval()
{
int b = num.top(); num.pop();
int a = num.top(); num.pop();
char c = op.top(); op.pop();
int x;
if (c == '+') x = a + b;
else if (c == '-') x = a - b;
else if (c == '*') x = a * b;
else x = a / b;
num.push(x);
}
int main()
{
unordered_map<char, int> pr{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};
string str;
cin >> str;
for (int i = 0; i < str.size(); i ++ )
{
auto c = str[i];
if (isdigit(c))
{
int x = 0, j = i;
while (j < str.size() && isdigit(str[j]))
x = x * 10 + str[j ++ ] - '0';
i = j - 1;
num.push(x);
}
else if (c == '(') op.push(c);
else if (c == ')')
{
while (op.top() != '(') eval();
op.pop();
}
else
{
while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval();
op.push(c);
}
}
while (op.size()) eval();
cout << num.top() << endl;
return 0;
}