A bug fix.

This commit is contained in:
krahets
2023-12-19 21:55:57 +08:00
parent d9686e57dd
commit 9a5ab776d6
9 changed files with 30 additions and 34 deletions

View File

@ -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());

View File

@ -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());

View File

@ -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());

View File

@ -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());