krahets 11 months ago
parent 60f0f80df6
commit ef0f9dba2e

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

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

Loading…
Cancel
Save