|
| 1 | +#include <cassert> |
| 2 | + |
| 3 | +struct Node { |
| 4 | + int val; |
| 5 | + Node *next; |
| 6 | + Node *prev; |
| 7 | + |
| 8 | + void SetNext(Node *n) { this->next = n; } |
| 9 | + void SetPrev(Node *p) { this->prev = p; } |
| 10 | +}; |
| 11 | + |
| 12 | +Node *Find(Node *head, int idx) { |
| 13 | + Node *curr = head; |
| 14 | + for (int i = 0; i < idx; i++) { |
| 15 | + curr = curr->next; |
| 16 | + } |
| 17 | + return curr; |
| 18 | +} |
| 19 | + |
| 20 | +Node *FindBack(Node *tail, int idx) { |
| 21 | + Node *curr = tail; |
| 22 | + for (int i = 0; i < idx; i++) { |
| 23 | + curr = curr->prev; |
| 24 | + } |
| 25 | + return curr; |
| 26 | +} |
| 27 | + |
| 28 | +void Append(Node &head, Node &new_node) { |
| 29 | + Node *curr = &head; |
| 30 | + while (curr->next != nullptr) { |
| 31 | + curr = curr->next; |
| 32 | + } |
| 33 | + curr->SetNext(&new_node); |
| 34 | + new_node.SetPrev(curr); |
| 35 | +} |
| 36 | + |
| 37 | +Node *Delete(Node *head, int val) { |
| 38 | + Node *curr = head; |
| 39 | + while (curr != nullptr) { |
| 40 | + if (curr->val == val) { |
| 41 | + Node *prev = curr->prev; |
| 42 | + Node *next = curr->next; |
| 43 | + if (prev != nullptr) { |
| 44 | + prev->next = next; |
| 45 | + } |
| 46 | + if (next != nullptr) { |
| 47 | + next->prev = prev; |
| 48 | + } |
| 49 | + if (prev != nullptr) { |
| 50 | + return head; |
| 51 | + } else { |
| 52 | + return next; |
| 53 | + } |
| 54 | + } |
| 55 | + curr = curr->next; |
| 56 | + } |
| 57 | + return head; |
| 58 | +} |
| 59 | + |
| 60 | +Node *Tail(Node *head) { |
| 61 | + Node *curr = head; |
| 62 | + while (curr->next != nullptr) { |
| 63 | + curr = curr->next; |
| 64 | + } |
| 65 | + return curr; |
| 66 | +} |
| 67 | + |
| 68 | +int main() { |
| 69 | + Node n0 = {5, nullptr, nullptr}; |
| 70 | + Node *head = &n0; |
| 71 | + Node n1 = {4, nullptr, nullptr}; |
| 72 | + Node n2 = {3, nullptr, nullptr}; |
| 73 | + Node n3 = {2, nullptr, nullptr}; |
| 74 | + Node n4 = {1, nullptr, nullptr}; |
| 75 | + Node n5 = {0, nullptr, nullptr}; |
| 76 | + Node n6 = {-1, nullptr, nullptr}; |
| 77 | + Node n7 = {-2, nullptr, nullptr}; |
| 78 | + Append(*head, n1); |
| 79 | + Append(*head, n2); |
| 80 | + Append(*head, n3); |
| 81 | + Append(*head, n4); |
| 82 | + Append(*head, n5); |
| 83 | + Append(*head, n6); |
| 84 | + Append(*head, n7); |
| 85 | + head = Delete(head, 5); |
| 86 | + head = Delete(head, 0); |
| 87 | + head = Delete(head, -2); |
| 88 | + Node *tail = Tail(head); |
| 89 | + |
| 90 | + assert(Find(head, 0)->val == 4); |
| 91 | + assert(Find(head, 1)->val == 3); |
| 92 | + assert(Find(head, 2)->val == 2); |
| 93 | + assert(Find(head, 3)->val == 1); |
| 94 | + assert(Find(head, 4)->val == -1); |
| 95 | + assert(Find(head, 5) == nullptr); |
| 96 | + assert(FindBack(tail, 0)->val == -1); |
| 97 | + assert(FindBack(tail, 1)->val == 1); |
| 98 | + assert(FindBack(tail, 2)->val == 2); |
| 99 | + assert(FindBack(tail, 3)->val == 3); |
| 100 | + assert(FindBack(tail, 4)->val == 4); |
| 101 | + assert(FindBack(tail, 4)->prev == nullptr); |
| 102 | + |
| 103 | + assert(Find(head, 0)->next->val == 3); |
| 104 | + assert(Find(head, 1)->next->next->val == 1); |
| 105 | + assert(Find(head, 2)->prev->val == 3); |
| 106 | + assert(Find(head, 4)->next == nullptr); |
| 107 | + assert(FindBack(tail, 1)->prev->prev->val == 3); |
| 108 | + |
| 109 | + Find(head, 0)->next->val = 30; |
| 110 | + assert(Find(head, 1)->val == 30); |
| 111 | + Find(head, 1)->next->val = Find(head, 0)->val + Find(head, 3)->val; |
| 112 | + assert(Find(head, 2)->val == 4 + 1); |
| 113 | + |
| 114 | + int sum = Find(head, 0)->val + Find(head, 1)->val + Find(head, 2)->val + |
| 115 | + Find(head, 3)->val + Find(head, 4)->val; |
| 116 | + assert(sum == 4 + 30 + 5 + 1 + -1); |
| 117 | + |
| 118 | + assert(Find(head, 0)->val + FindBack(tail, 0)->val == 4 + -1); |
| 119 | + assert(Find(head, 2)->next->val == FindBack(tail, 1)->val); |
| 120 | + assert(Find(head, 0)->prev == FindBack(tail, 4)->prev); |
| 121 | + |
| 122 | + return 0; |
| 123 | +} |
0 commit comments