This commit is contained in:
krahets 2023-12-19 21:57:51 +08:00
parent 60f0f80df6
commit ef0f9dba2e
2 changed files with 23 additions and 27 deletions

View file

@ -446,7 +446,7 @@ comments: true
}
/* 出队 */
void pop() {
int pop() {
int num = peek();
// 删除头节点
ListNode *tmp = front;
@ -454,6 +454,7 @@ comments: true
// 释放内存
delete tmp;
queSize--;
return num;
}
/* 访问队首元素 */
@ -1093,12 +1094,13 @@ comments: true
}
/* 出队 */
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;
}
/* 打印队列 */
@ -1345,11 +1347,12 @@ comments: true
}
/* 出队 */
void pop() {
int pop() {
int num = peek();
// 队首指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % queCapacity;
queSize--;
return num;
}
/* 访问队首元素 */
@ -2014,11 +2017,12 @@ comments: true
}
/* 出队 */
void pop(ArrayQueue *queue) {
int pop(ArrayQueue *queue) {
int num = peek(queue);
// 队首指针向后移动一位,若越过尾部则返回到数组头部
queue->front = (queue->front + 1) % queue->queCapacity;
queue->queSize--;
return num;
}
```

View file

@ -426,13 +426,14 @@ comments: true
}
/* 出栈 */
void pop() {
int pop() {
int num = top();
ListNode *tmp = stackTop;
stackTop = stackTop->next;
// 释放内存
delete tmp;
stkSize--;
return num;
}
/* 访问栈顶元素 */
@ -962,26 +963,16 @@ comments: true
/* 获取栈的长度 */
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; // 更新新加节点数据域
@ -989,13 +980,17 @@ comments: true
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;
@ -1165,9 +1160,10 @@ comments: true
}
/* 出栈 */
void pop() {
int oldTop = top();
int pop() {
int num = top();
stack.pop_back();
return num;
}
/* 访问栈顶元素 */
@ -1607,7 +1603,7 @@ comments: true
/* 入栈 */
void push(ArrayStack *stack, int num) {
if (stack->size == MAX_SIZE) {
printf("stack is full.\n");
printf("栈已满\n");
return;
}
stack->data[stack->size] = num;
@ -1617,7 +1613,7 @@ comments: true
/* 访问栈顶元素 */
int peek(ArrayStack *stack) {
if (stack->size == 0) {
printf("stack is empty.\n");
printf("栈为空\n");
return INT_MAX;
}
return stack->data[stack->size - 1];
@ -1625,10 +1621,6 @@ comments: true
/* 出栈 */
int pop(ArrayStack *stack) {
if (stack->size == 0) {
printf("stack is empty.\n");
return INT_MAX;
}
int val = peek(stack);
stack->size--;
return val;