Skip to content

Commit 9a5ab77

Browse files
committed
A bug fix.
1 parent d9686e5 commit 9a5ab77

File tree

9 files changed

+30
-34
lines changed

9 files changed

+30
-34
lines changed

codes/c/chapter_stack_and_queue/array_queue.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ void push(ArrayQueue *queue, int num) {
6666
}
6767

6868
/* 出队 */
69-
void pop(ArrayQueue *queue) {
69+
int pop(ArrayQueue *queue) {
7070
int num = peek(queue);
7171
// 队首指针向后移动一位,若越过尾部则返回到数组头部
7272
queue->front = (queue->front + 1) % queue->queCapacity;
7373
queue->queSize--;
74+
return num;
7475
}
7576

7677
/* Driver Code */
@@ -93,7 +94,7 @@ int main() {
9394
printf("队首元素 peek = %d\r\n", peekNum);
9495

9596
/* 元素出队 */
96-
pop(queue);
97+
peekNum = pop(queue);
9798
printf("出队元素 pop = %d ,出队后 queue = ", peekNum);
9899
printArray(queue->nums, queue->queSize);
99100

codes/c/chapter_stack_and_queue/array_stack.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool isEmpty(ArrayStack *stack) {
4242
/* 入栈 */
4343
void push(ArrayStack *stack, int num) {
4444
if (stack->size == MAX_SIZE) {
45-
printf("stack is full.\n");
45+
printf("栈已满\n");
4646
return;
4747
}
4848
stack->data[stack->size] = num;
@@ -52,18 +52,14 @@ void push(ArrayStack *stack, int num) {
5252
/* 访问栈顶元素 */
5353
int peek(ArrayStack *stack) {
5454
if (stack->size == 0) {
55-
printf("stack is empty.\n");
55+
printf("栈为空\n");
5656
return INT_MAX;
5757
}
5858
return stack->data[stack->size - 1];
5959
}
6060

6161
/* 出栈 */
6262
int pop(ArrayStack *stack) {
63-
if (stack->size == 0) {
64-
printf("stack is empty.\n");
65-
return INT_MAX;
66-
}
6763
int val = peek(stack);
6864
stack->size--;
6965
return val;

codes/c/chapter_stack_and_queue/linkedlist_queue.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ int peek(LinkedListQueue *queue) {
6767
}
6868

6969
/* 出队 */
70-
void pop(LinkedListQueue *queue) {
70+
int pop(LinkedListQueue *queue) {
7171
int num = peek(queue);
7272
ListNode *tmp = queue->front;
7373
queue->front = queue->front->next;
7474
free(tmp);
7575
queue->queSize--;
76+
return num;
7677
}
7778

7879
/* 打印队列 */
@@ -108,7 +109,7 @@ int main() {
108109
printf("队首元素 peek = %d\r\n", peekNum);
109110

110111
/* 元素出队 */
111-
pop(queue);
112+
peekNum = pop(queue);
112113
printf("出队元素 pop = %d ,出队后 queue = ", peekNum);
113114
printLinkedListQueue(queue);
114115

codes/c/chapter_stack_and_queue/linkedlist_stack.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,34 @@ void delLinkedListStack(LinkedListStack *s) {
3232

3333
/* 获取栈的长度 */
3434
int size(LinkedListStack *s) {
35-
assert(s);
3635
return s->size;
3736
}
3837

3938
/* 判断栈是否为空 */
4039
bool isEmpty(LinkedListStack *s) {
41-
assert(s);
4240
return size(s) == 0;
4341
}
4442

45-
/* 访问栈顶元素 */
46-
int peek(LinkedListStack *s) {
47-
assert(s);
48-
assert(size(s) != 0);
49-
return s->top->val;
50-
}
51-
5243
/* 入栈 */
5344
void push(LinkedListStack *s, int num) {
54-
assert(s);
5545
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
5646
node->next = s->top; // 更新新加节点指针域
5747
node->val = num; // 更新新加节点数据域
5848
s->top = node; // 更新栈顶
5949
s->size++; // 更新栈大小
6050
}
6151

62-
/* 出栈 */
63-
int pop(LinkedListStack *s) {
52+
/* 访问栈顶元素 */
53+
int peek(LinkedListStack *s) {
6454
if (s->size == 0) {
65-
printf("stack is empty.\n");
55+
printf("栈为空\n");
6656
return INT_MAX;
6757
}
68-
assert(s);
58+
return s->top->val;
59+
}
60+
61+
/* 出栈 */
62+
int pop(LinkedListStack *s) {
6963
int val = peek(s);
7064
ListNode *tmp = s->top;
7165
s->top = s->top->next;

codes/cpp/chapter_stack_and_queue/array_queue.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ class ArrayQueue {
5656
}
5757

5858
/* 出队 */
59-
void pop() {
59+
int pop() {
6060
int num = peek();
6161
// 队首指针向后移动一位,若越过尾部则返回到数组头部
6262
front = (front + 1) % queCapacity;
6363
queSize--;
64+
return num;
6465
}
6566

6667
/* 访问队首元素 */
@@ -101,7 +102,7 @@ int main() {
101102
cout << "队首元素 peek = " << peek << endl;
102103

103104
/* 元素出队 */
104-
queue->pop();
105+
peek = queue->pop();
105106
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
106107
printVector(queue->toVector());
107108

codes/cpp/chapter_stack_and_queue/array_stack.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ class ArrayStack {
2828
}
2929

3030
/* 出栈 */
31-
void pop() {
32-
int oldTop = top();
31+
int pop() {
32+
int num = top();
3333
stack.pop_back();
34+
return num;
3435
}
3536

3637
/* 访问栈顶元素 */
@@ -65,7 +66,7 @@ int main() {
6566
cout << "栈顶元素 top = " << top << endl;
6667

6768
/* 元素出栈 */
68-
stack->pop();
69+
top = stack->pop();
6970
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
7071
printVector(stack->toVector());
7172

codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ class LinkedListQueue {
5252
}
5353

5454
/* 出队 */
55-
void pop() {
55+
int pop() {
5656
int num = peek();
5757
// 删除头节点
5858
ListNode *tmp = front;
5959
front = front->next;
6060
// 释放内存
6161
delete tmp;
6262
queSize--;
63+
return num;
6364
}
6465

6566
/* 访问队首元素 */
@@ -100,7 +101,7 @@ int main() {
100101
cout << "队首元素 peek = " << peek << endl;
101102

102103
/* 元素出队 */
103-
queue->pop();
104+
peek = queue->pop();
104105
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
105106
printVector(queue->toVector());
106107

codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ class LinkedListStack {
4242
}
4343

4444
/* 出栈 */
45-
void pop() {
45+
int pop() {
4646
int num = top();
4747
ListNode *tmp = stackTop;
4848
stackTop = stackTop->next;
4949
// 释放内存
5050
delete tmp;
5151
stkSize--;
52+
return num;
5253
}
5354

5455
/* 访问栈顶元素 */
@@ -89,7 +90,7 @@ int main() {
8990
cout << "栈顶元素 top = " << top << endl;
9091

9192
/* 元素出栈 */
92-
stack->pop();
93+
top = stack->pop();
9394
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
9495
printVector(stack->toVector());
9596

docs/chapter_preface/about_the_book.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@
4343

4444
本书倡导手脑并用的学习方式,在这一点上深受[《动手学深度学习》](https://github.com/d2l-ai/d2l-zh)的启发。在此向各位读者强烈推荐这本优秀的著作。
4545

46-
衷心感谢我的父母,正是你们一直以来的支持与鼓励,让我有机会做这件富有趣味的事。
46+
**衷心感谢我的父母,正是你们一直以来的支持与鼓励,让我有机会做这件富有趣味的事**

0 commit comments

Comments
 (0)