This commit is contained in:
krahets 2023-03-02 18:50:29 +08:00
parent 851107c4eb
commit 122805bdc9

View file

@ -453,17 +453,17 @@ comments: true
```cpp title="linkedlist_deque.cpp" ```cpp title="linkedlist_deque.cpp"
/* 双向链表结点 */ /* 双向链表结点 */
struct DoubleListNode { struct DoublyListNode {
int val; // 结点值 int val; // 结点值
DoubleListNode *next; // 后继结点指针 DoublyListNode *next; // 后继结点指针
DoubleListNode *prev; // 前驱结点指针 DoublyListNode *prev; // 前驱结点指针
DoubleListNode(int val) : val(val), prev(nullptr), next(nullptr) {} DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {}
}; };
/* 基于双向链表实现的双向队列 */ /* 基于双向链表实现的双向队列 */
class LinkedListDeque { class LinkedListDeque {
private: private:
DoubleListNode *front, *rear; // 头结点 front ,尾结点 rear DoublyListNode *front, *rear; // 头结点 front ,尾结点 rear
int queSize = 0; // 双向队列的长度 int queSize = 0; // 双向队列的长度
public: public:
@ -473,7 +473,7 @@ comments: true
/* 析构方法 */ /* 析构方法 */
~LinkedListDeque() { ~LinkedListDeque() {
// 释放内存 // 释放内存
DoubleListNode *pre, *cur = front; DoublyListNode *pre, *cur = front;
while (cur != nullptr) { while (cur != nullptr) {
pre = cur; pre = cur;
cur = cur->next; cur = cur->next;
@ -493,7 +493,7 @@ comments: true
/* 入队操作 */ /* 入队操作 */
void push(int num, bool isFront) { void push(int num, bool isFront) {
DoubleListNode *node = new DoubleListNode(num); DoublyListNode *node = new DoublyListNode(num);
// 若链表为空,则令 front, rear 都指向 node // 若链表为空,则令 front, rear 都指向 node
if (isEmpty()) if (isEmpty())
front = rear = node; front = rear = node;
@ -533,7 +533,7 @@ comments: true
if (isFront) { if (isFront) {
val = front->val; // 暂存头结点值 val = front->val; // 暂存头结点值
// 删除头结点 // 删除头结点
DoubleListNode *fNext = front->next; DoublyListNode *fNext = front->next;
if (fNext != nullptr) { if (fNext != nullptr) {
fNext->prev = nullptr; fNext->prev = nullptr;
front->next = nullptr; front->next = nullptr;
@ -543,7 +543,7 @@ comments: true
} else { } else {
val = rear->val; // 暂存尾结点值 val = rear->val; // 暂存尾结点值
// 删除尾结点 // 删除尾结点
DoubleListNode *rPrev = rear->prev; DoublyListNode *rPrev = rear->prev;
if (rPrev != nullptr) { if (rPrev != nullptr) {
rPrev->next = nullptr; rPrev->next = nullptr;
rear->prev = nullptr; rear->prev = nullptr;
@ -576,7 +576,7 @@ comments: true
/* 返回数组用于打印 */ /* 返回数组用于打印 */
vector<int> toVector() { vector<int> toVector() {
DoubleListNode *node = front; DoublyListNode *node = front;
vector<int> res(size()); vector<int> res(size());
for (int i = 0; i < res.size(); i++) { for (int i = 0; i < res.size(); i++) {
res[i] = node->val; res[i] = node->val;