C语言 在堆栈中使用“push”和“pop”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18755948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using “push” and “pop” in a stack
提问by walkirie27
I have an assignment that is asking me to fill up a stack with random variables and pop them out in a FILO order. Whilst I managed to get it to fill the stack, it seems to be popping out the last element and nothing else. I'm not sure why. Any help would be appreciated.
我有一项任务要求我用随机变量填充堆栈并以 FILO 顺序弹出它们。虽然我设法让它填满堆栈,但它似乎弹出了最后一个元素而没有其他元素。我不知道为什么。任何帮助,将不胜感激。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define STACK_SIZE 10
#define STACK_EMPTY -1
void push(char [], // input/ouput - the stack
char, // input - data being pushed onto the stack
int *, // input/output - pointer to the index of the top of stack
int); // constant - maximum size of stack
char // output - data being popped out from the stack
pop(char [], // input/output - the stack
int *); // input/output - pointer to the index of the top of stack
void push(char stack[],char item,int *top,int max_size){
stack[*top++] =item;
}
char pop(char stack[],int *top){
return stack[*top--];
}
int main(){
char s[STACK_SIZE];
int s_top = STACK_EMPTY; // Pointer points to the index of the top of the stack
char randChar = ' ';
int i = 0;
int j=0;
int randNum = 0;
srand(time(NULL));
for (i = 0; i < STACK_SIZE; i++){
randNum = 33 + (int)(rand() % ((126-33)+ 1 ));
randChar = (char) randNum;
push(s,randChar, &s_top, STACK_SIZE);
printf ("Random char: %c\n", randChar);
}
printf("-----------\n");
for(j=STACK_SIZE; j>0; j--){
printf("Random chars:%c\n", pop(s, &s_top));
}
return 0;
}
回答by phoxis
Your push should be
你的推应该是
(*top)++;
stack[*top] = value;
That is first increment to the next empty position and then insert. The topvariable always points to the top element. Therefore to push, first increment then assign. To pop, first extract the value at top and then decrement.
即先递增到下一个空位置,然后再插入。该top变量始终指向顶部元素。因此要推动,首先增加然后分配。要弹出,首先提取顶部的值,然后递减。
Note: the above line can be clubbed to stack[++(*top)] = value
注意:上面的线可以用棍棒打到 stack[++(*top)] = value
In the current code, at the first push, your code with stack[*top++] = item, with the post increment attempts to assign the value to the current value of *topwhich is -1and then increment, which is wrong.
在当前代码中,在第一次 push 时,您的代码带有stack[*top++] = item, 后增量尝试将值分配给*topis的当前值,-1然后增量,这是错误的。
With respect to this modification of push routine the pop routine is okay.
对于推送例程的这种修改,弹出例程是可以的。
回答by P0W
I'll mix both the answers (one got deleted just now ) :
我将混合两个答案(一个刚刚被删除):
You've have to fix both pushand pop
你必须同时修复push和pop
void push(char stack[],char item,int *top,int max_size){
stack[++(*top)] = item;
}
char pop(char stack[],int *top){
return stack[(*top)--];
}
Will now give expected result
现在将给出预期的结果
回答by John Bode
Postfix ++and --have higher precedence than unary *, so in order to increment the thing that toppoints to, you need to write (*top)++and (*top)--; *top++will advance the pointer, which is not what you want.
后缀++和--比一元有更高的优先级*,所以为了增加top指向的东西,你需要写(*top)++和(*top)--; *top++将使指针前进,这不是您想要的。
Secondly, the stack pointer should always point to the last thing added to the stack, so you want to increment the stack pointer beforewriting to the stack:
其次,堆栈指针应始终指向添加到堆栈的最后一个内容,因此您希望在写入堆栈之前增加堆栈指针:
stack[++(*top)] = value;
Prefix ++has the same precedence as unary *, so in this case the parentheses aren't strictly necessary; the operations are applied left-to-right, so ++*topis interpreted as ++(*top), but the parens help make things clear.
Prefix++与 unary 具有相同的优先级*,因此在这种情况下,括号不是绝对必要的;操作是从左到右应用的,因此++*top被解释为++(*top),但括号有助于使事情变得清晰。
Push and pop should always be the inverse of each other; if you push with ++(*top), you need to pop with (*top)--.
push 和 pop 应该始终是彼此的倒数;如果你用推++(*top),你需要弹出(*top)--。

![C语言 如何在C中找到argv[]的长度](/res/img/loading.gif)