数组实现链表,栈,队列
#include<bits/stdc++.h> using namespace std; const int N = 2e5 + 9; int e[N], p[N], head, tail; void del(int x){ if (e[x] == -1){ e[p[x]] = -1; } else { e[p[x]] = e[x], p[e[x]] = p[x]; } } void insert_front(int y, int x){ e[x] = y, p[x] = p[y], p[e[x]] = x, e[p[x]] = x; } void insert_back(int y, int x){ if (e[y] == -1){ e[y] = x; p[x] = y; e[x] = -1; } else{ e[x] = e[y]; p[x] = y; e[y] = x; p[e[x]] = x; } } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++){ e[i] = i + 1; p[i] = i - 1; } e[0] = 1, e[n] = -1; head = 0, tail = n; e[head] = 0, tail = n; e[head] = 1, e[tail] = -1; for (int i = 1; i <= m; i++){ int x, y, z; cin >> x >> y >> z; if (z)del(x), insert_front(y, x); else del(x), insert_back(y, x); } for (int i = e[0]; i != -1; i = e[i])cout << i << " "; return 0; }
栈
#include<bits/stdc++.h> using namespace std; const int N = 105; char stk[N]; int top; char s[N]; int main() { int n; cin >> n; cin >> s + 1; for (int i = 1; i <= n; i++){ if (s[i] == ')'){ if (top&&stk[top] == ')'){ top--; continue; } } stk[++top] = s[i]; } if (top)cout << "No" << '\n'; else cout << "Yes" << '\n'; return 0; }
队列
入队:q[++h]=x;
出队:hh++;
#include<bits/stdc++.h> using namespace std; const int N = 2e3 + 9; int q[N], hh = 1, tt = 0; int main() { int m, n,ans=0; cin >> m >> n; for (int i = 1; i <= n; ++i){ int x; cin >> x; bool tag = false; for (int j = hh; j <= tt; ++j)if (q[j] == x)tag = true; if (tag)continue; if (tt - hh + 1 == m)hh++; q[++tt] = x; ans++; } cout << ans << "\n"; return 0; }