Skip to content

Commit

Permalink
A bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Dec 19, 2023
1 parent d9686e5 commit 9a5ab77
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 34 deletions.
5 changes: 3 additions & 2 deletions codes/c/chapter_stack_and_queue/array_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);

Expand Down
8 changes: 2 additions & 6 deletions codes/c/chapter_stack_and_queue/array_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,18 +52,14 @@ 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];
}

/* 出栈 */
int pop(ArrayStack *stack) {
if (stack->size == 0) {
printf("stack is empty.\n");
return INT_MAX;
}
int val = peek(stack);
stack->size--;
return val;
Expand Down
5 changes: 3 additions & 2 deletions codes/c/chapter_stack_and_queue/linkedlist_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

/* 元素出队 */
pop(queue);
peekNum = pop(queue);
printf("出队元素 pop = %d ,出队后 queue = ", peekNum);
printLinkedListQueue(queue);

Expand Down
22 changes: 8 additions & 14 deletions codes/c/chapter_stack_and_queue/linkedlist_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,34 @@ 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; // 更新新加节点数据域
s->top = node; // 更新栈顶
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;
Expand Down
5 changes: 3 additions & 2 deletions codes/cpp/chapter_stack_and_queue/array_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ class ArrayQueue {
}

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

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

/* 元素出队 */
queue->pop();
peek = queue->pop();
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
printVector(queue->toVector());

Expand Down
7 changes: 4 additions & 3 deletions codes/cpp/chapter_stack_and_queue/array_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class ArrayStack {
}

/* 出栈 */
void pop() {
int oldTop = top();
int pop() {
int num = top();
stack.pop_back();
return num;
}

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

/* 元素出栈 */
stack->pop();
top = stack->pop();
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
printVector(stack->toVector());

Expand Down
5 changes: 3 additions & 2 deletions codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ class LinkedListQueue {
}

/* 出队 */
void pop() {
int pop() {
int num = peek();
// 删除头节点
ListNode *tmp = front;
front = front->next;
// 释放内存
delete tmp;
queSize--;
return num;
}

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

/* 元素出队 */
queue->pop();
peek = queue->pop();
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
printVector(queue->toVector());

Expand Down
5 changes: 3 additions & 2 deletions codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ class LinkedListStack {
}

/* 出栈 */
void pop() {
int pop() {
int num = top();
ListNode *tmp = stackTop;
stackTop = stackTop->next;
// 释放内存
delete tmp;
stkSize--;
return num;
}

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

/* 元素出栈 */
stack->pop();
top = stack->pop();
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
printVector(stack->toVector());

Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_preface/about_the_book.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@

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

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

0 comments on commit 9a5ab77

Please sign in to comment.