1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <iostream> using namespace std;
const int N = 1e5 + 10; struct node { int val; node* next; };
bool insert(node* dummyhead,int k, int d) { if (k < 0) return false; node* p = dummyhead; for (int i = 0; i < k; i++) { p = p->next; if (p == NULL) return false; } node* tmp = new node; tmp->val = d; tmp->next = p->next; p->next = tmp; return true; } bool remove(node* dummyhead,int k) { if (k <= 0) return false; node* p = dummyhead; for (int i = 0; i < k - 1; i++) { p = p->next; if (p == NULL) return false; } p->next = p->next->next; } int main() { int n; cin >> n; node* dummyhead = new node; dummyhead->next = NULL; node* p = dummyhead; for (int i = 0; i < n; i++) { int x; cin >> x; node* tmp = new node; tmp->val = x; tmp->next = NULL; p->next = tmp; p = p->next; }
int m; cin >> m; for (int i = 0; i < m; i++) { int op; cin >> op; if (op == 0) { int k, d; cin >> k >> d; insert(dummyhead, k, d); } else { int d; cin >> d; remove(dummyhead, d); } } node* t = dummyhead->next; while (t) { cout << t->val<<" "; t = t->next; } }
|