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