A static linked list
Yesterday’s problem was resolved after repeated interruptions to debug:
#include <iostream> using namespace std; const int N = 100010; int idx, head, e[N], ne[N]; Void add_to_head(int x) {e[idx] = x; ne[idx] = head; head = idx++; Void delete_after_k(int k) {ne[k] = ne[ne[k]]; Void add_after_k(int x, int k) {e[idx] = x; ne[idx] = ne[k]; ne[k] = idx++; } int main() { int head = -1; int idx = 0; int m; cin >> m; while (m--) { int k, x; char op; cin >> op; if (op == 'H') { cin >> x; add_to_head(x); } else if (op == 'D') { cin >> k; if (! k) head = ne[head]; delete_after_k(k - 1); } else { int k, x; cin >> k >> x; add_after_k(x, k - 1); } } for (int i = head; i ! = 1; i = ne[i]) { cout << e[i] << ' '; } cout << endl; return 0; }Copy the code
At first glance, there is nothing wrong with the list initialization.
int main() {
int head = -1;
int idx = 0;
int m;
cin >> m;
while (m--) {
Copy the code
Fix it:
int main() {
head = -1;
idx = 0;
int m;
cin >> m;
while (m--) {
Copy the code
In the outermost domain, we define head and IDx. We would have called init, but assigning -1 to head and 0 to IDx would have the same effect. The mistake is that we shouldn’t define them with int anymore, causing the compiler to think they’re local variables. Head =-1; idx=0; add_to_head =0; ne[idx] =0; Ne [I] is always pointing to the original 9, and 99999 will always be output.
Conclusion: in the global definition of the variable, after the initialization of the time do not add type, otherwise only local effect.