From 9a5ab776d63c93fc69fec4888a8ebd7fe4548630 Mon Sep 17 00:00:00 2001 From: krahets Date: Tue, 19 Dec 2023 21:55:57 +0800 Subject: [PATCH] A bug fix. --- codes/c/chapter_stack_and_queue/array_queue.c | 5 +++-- codes/c/chapter_stack_and_queue/array_stack.c | 8 ++----- .../linkedlist_queue.c | 5 +++-- .../linkedlist_stack.c | 22 +++++++------------ .../chapter_stack_and_queue/array_queue.cpp | 5 +++-- .../chapter_stack_and_queue/array_stack.cpp | 7 +++--- .../linkedlist_queue.cpp | 5 +++-- .../linkedlist_stack.cpp | 5 +++-- docs/chapter_preface/about_the_book.md | 2 +- 9 files changed, 30 insertions(+), 34 deletions(-) diff --git a/codes/c/chapter_stack_and_queue/array_queue.c b/codes/c/chapter_stack_and_queue/array_queue.c index 9c7ea3e9b1..4a00dfdb63 100644 --- a/codes/c/chapter_stack_and_queue/array_queue.c +++ b/codes/c/chapter_stack_and_queue/array_queue.c @@ -66,11 +66,12 @@ void push(ArrayQueue *queue, int num) { } /* 出队 */ -void pop(ArrayQueue *queue) { +int pop(ArrayQueue *queue) { int num = peek(queue); // 队首指针向后移动一位,若越过尾部则返回到数组头部 queue->front = (queue->front + 1) % queue->queCapacity; queue->queSize--; + return num; } /* Driver Code */ @@ -93,7 +94,7 @@ int main() { printf("队首元素 peek = %d\r\n", peekNum); /* 元素出队 */ - pop(queue); + peekNum = pop(queue); printf("出队元素 pop = %d ,出队后 queue = ", peekNum); printArray(queue->nums, queue->queSize); diff --git a/codes/c/chapter_stack_and_queue/array_stack.c b/codes/c/chapter_stack_and_queue/array_stack.c index 3c86558835..807c9bca4d 100644 --- a/codes/c/chapter_stack_and_queue/array_stack.c +++ b/codes/c/chapter_stack_and_queue/array_stack.c @@ -42,7 +42,7 @@ bool isEmpty(ArrayStack *stack) { /* 入栈 */ void push(ArrayStack *stack, int num) { if (stack->size == MAX_SIZE) { - printf("stack is full.\n"); + printf("栈已满\n"); return; } stack->data[stack->size] = num; @@ -52,7 +52,7 @@ void push(ArrayStack *stack, int num) { /* 访问栈顶元素 */ int peek(ArrayStack *stack) { if (stack->size == 0) { - printf("stack is empty.\n"); + printf("栈为空\n"); return INT_MAX; } return stack->data[stack->size - 1]; @@ -60,10 +60,6 @@ int peek(ArrayStack *stack) { /* 出栈 */ int pop(ArrayStack *stack) { - if (stack->size == 0) { - printf("stack is empty.\n"); - return INT_MAX; - } int val = peek(stack); stack->size--; return val; diff --git a/codes/c/chapter_stack_and_queue/linkedlist_queue.c b/codes/c/chapter_stack_and_queue/linkedlist_queue.c index df73857fcb..9ffdf3b49d 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_queue.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_queue.c @@ -67,12 +67,13 @@ int peek(LinkedListQueue *queue) { } /* 出队 */ -void pop(LinkedListQueue *queue) { +int pop(LinkedListQueue *queue) { int num = peek(queue); ListNode *tmp = queue->front; queue->front = queue->front->next; free(tmp); queue->queSize--; + return num; } /* 打印队列 */ @@ -108,7 +109,7 @@ int main() { printf("队首元素 peek = %d\r\n", peekNum); /* 元素出队 */ - pop(queue); + peekNum = pop(queue); printf("出队元素 pop = %d ,出队后 queue = ", peekNum); printLinkedListQueue(queue); diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index 6319fb2c8a..25795b2862 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -32,26 +32,16 @@ void delLinkedListStack(LinkedListStack *s) { /* 获取栈的长度 */ int size(LinkedListStack *s) { - assert(s); return s->size; } /* 判断栈是否为空 */ bool isEmpty(LinkedListStack *s) { - assert(s); return size(s) == 0; } -/* 访问栈顶元素 */ -int peek(LinkedListStack *s) { - assert(s); - assert(size(s) != 0); - return s->top->val; -} - /* 入栈 */ void push(LinkedListStack *s, int num) { - assert(s); ListNode *node = (ListNode *)malloc(sizeof(ListNode)); node->next = s->top; // 更新新加节点指针域 node->val = num; // 更新新加节点数据域 @@ -59,13 +49,17 @@ void push(LinkedListStack *s, int num) { s->size++; // 更新栈大小 } -/* 出栈 */ -int pop(LinkedListStack *s) { +/* 访问栈顶元素 */ +int peek(LinkedListStack *s) { if (s->size == 0) { - printf("stack is empty.\n"); + printf("栈为空\n"); return INT_MAX; } - assert(s); + return s->top->val; +} + +/* 出栈 */ +int pop(LinkedListStack *s) { int val = peek(s); ListNode *tmp = s->top; s->top = s->top->next; diff --git a/codes/cpp/chapter_stack_and_queue/array_queue.cpp b/codes/cpp/chapter_stack_and_queue/array_queue.cpp index 27c59463dc..f12e64e5f5 100644 --- a/codes/cpp/chapter_stack_and_queue/array_queue.cpp +++ b/codes/cpp/chapter_stack_and_queue/array_queue.cpp @@ -56,11 +56,12 @@ class ArrayQueue { } /* 出队 */ - void pop() { + int pop() { int num = peek(); // 队首指针向后移动一位,若越过尾部则返回到数组头部 front = (front + 1) % queCapacity; queSize--; + return num; } /* 访问队首元素 */ @@ -101,7 +102,7 @@ int main() { cout << "队首元素 peek = " << peek << endl; /* 元素出队 */ - queue->pop(); + peek = queue->pop(); cout << "出队元素 pop = " << peek << ",出队后 queue = "; printVector(queue->toVector()); diff --git a/codes/cpp/chapter_stack_and_queue/array_stack.cpp b/codes/cpp/chapter_stack_and_queue/array_stack.cpp index c5e5663c5a..c59e076b39 100644 --- a/codes/cpp/chapter_stack_and_queue/array_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/array_stack.cpp @@ -28,9 +28,10 @@ class ArrayStack { } /* 出栈 */ - void pop() { - int oldTop = top(); + int pop() { + int num = top(); stack.pop_back(); + return num; } /* 访问栈顶元素 */ @@ -65,7 +66,7 @@ int main() { cout << "栈顶元素 top = " << top << endl; /* 元素出栈 */ - stack->pop(); + top = stack->pop(); cout << "出栈元素 pop = " << top << ",出栈后 stack = "; printVector(stack->toVector()); diff --git a/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp b/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp index a1769359a9..0d2bd58e05 100644 --- a/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp +++ b/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp @@ -52,7 +52,7 @@ class LinkedListQueue { } /* 出队 */ - void pop() { + int pop() { int num = peek(); // 删除头节点 ListNode *tmp = front; @@ -60,6 +60,7 @@ class LinkedListQueue { // 释放内存 delete tmp; queSize--; + return num; } /* 访问队首元素 */ @@ -100,7 +101,7 @@ int main() { cout << "队首元素 peek = " << peek << endl; /* 元素出队 */ - queue->pop(); + peek = queue->pop(); cout << "出队元素 pop = " << peek << ",出队后 queue = "; printVector(queue->toVector()); diff --git a/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp b/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp index c51a46b786..dbf1f67244 100644 --- a/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp @@ -42,13 +42,14 @@ class LinkedListStack { } /* 出栈 */ - void pop() { + int pop() { int num = top(); ListNode *tmp = stackTop; stackTop = stackTop->next; // 释放内存 delete tmp; stkSize--; + return num; } /* 访问栈顶元素 */ @@ -89,7 +90,7 @@ int main() { cout << "栈顶元素 top = " << top << endl; /* 元素出栈 */ - stack->pop(); + top = stack->pop(); cout << "出栈元素 pop = " << top << ",出栈后 stack = "; printVector(stack->toVector()); diff --git a/docs/chapter_preface/about_the_book.md b/docs/chapter_preface/about_the_book.md index 5d1e08f61a..f79e13b450 100644 --- a/docs/chapter_preface/about_the_book.md +++ b/docs/chapter_preface/about_the_book.md @@ -43,4 +43,4 @@ 本书倡导手脑并用的学习方式,在这一点上深受[《动手学深度学习》](https://github.com/d2l-ai/d2l-zh)的启发。在此向各位读者强烈推荐这本优秀的著作。 -衷心感谢我的父母,正是你们一直以来的支持与鼓励,让我有机会做这件富有趣味的事。 +**衷心感谢我的父母,正是你们一直以来的支持与鼓励,让我有机会做这件富有趣味的事**。