题目(材料题)
下面C++代码实现双向链表。
|
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 |
// 节点结构体 struct Node { int data; Node* prev; Node* next; }; // 双向链表结构体 struct DoubleLink { Node* head; Node* tail; int size; DoubleLink() { head = nullptr; tail = nullptr; size = 0; } ~DoubleLink() { Node* curr = head; while (curr) { Node* next = curr->next; delete curr; curr = next; } } // 判断链表是否为空 bool is_empty() const { _______________________ } void append(int data) { Node* newNode = new Node{ data, nullptr, nullptr }; if (is_empty()) { head = tail = newNode; } else { _______________________ } ++size; } }; |

关注我们