Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用“;推动&x201D;和“;流行音乐”;成堆_C - Fatal编程技术网

使用“;推动&x201D;和“;流行音乐”;成堆

使用“;推动&x201D;和“;流行音乐”;成堆,c,C,我有一个任务,要求我用随机变量填充一个堆栈,然后按FILO顺序将其弹出。虽然我设法让它填满了堆栈,但它似乎弹出了最后一个元素,而没有其他东西。我不知道为什么。任何帮助都将不胜感激 #include <stdio.h> #include <stdlib.h> #include <time.h> #define STACK_SIZE 10 #define STACK_EMPTY -1 void push(char [], // input/ouput - the

我有一个任务,要求我用随机变量填充一个堆栈,然后按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;
}
#包括
#包括
#包括
#定义堆栈大小为10
#将堆栈定义为空-1
void push(char[],//输入/输出-堆栈
char,//输入-数据被推送到堆栈上
int*,//输入/输出-指向堆栈顶部索引的指针
int);//常量-堆栈的最大大小
char//output-从堆栈中弹出的数据
pop(char[],//输入/输出-堆栈
int*);//输入/输出-指向堆栈顶部索引的指针
无效推送(字符堆栈[],字符项,int*top,int max_size){
堆栈[*top++]=项;
}
char-pop(char-stack[],int*top){
返回堆栈[*顶部--];
}
int main(){
字符s[堆栈大小];
int s_top=STACK_EMPTY;//指针指向堆栈顶部的索引
char randChar='';
int i=0;
int j=0;
int randNum=0;
srand(时间(空));
对于(i=0;i0;j--){
printf(“随机字符:%c\n”,pop(s,&s_-top));
}
返回0;
}
您的推送应该是

(*top)++;
stack[*top] = value;
即首先递增到下一个空位置,然后插入。
top
变量始终指向top元素。因此,要推送,首先递增,然后分配。要弹出,首先提取顶部的值,然后递减

注意:上面的行可以用棍棒插入
stack[++(*top)]=value

在当前代码中,在第一次推送时,带有
堆栈[*top++]=item
,带有后期增量的代码尝试将值分配给
*top
的当前值,即
-1
,然后递增,这是错误的

关于这个推送程序的修改,弹出程序还可以。

我将混合两个答案(刚才删除了一个):

您必须同时修复
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)--];
}
现在将给出预期结果


See和后缀
++
-
比一元
*
具有更高的优先级,因此为了增加
top
指向的内容,需要编写
(*top)+
(*top)
<代码>*top++将使指针前进,而这不是您想要的

其次,堆栈指针应始终指向添加到堆栈中的最后一个对象,因此您希望在写入堆栈之前增加堆栈指针:

stack[++(*top)] = value;
前缀
++
与一元
*
具有相同的优先级,因此在这种情况下,括号不是绝对必要的;这些操作是从左到右应用的,因此
++*top
被解释为
++(*top)
,但是paren有助于把事情弄清楚


推送和弹出应该始终是相反的;如果使用
++(*top)
,则需要使用
(*top)--
弹出

stack[++(*top)]=项在一个step@GrijeshChauhan:是的,这是正确的,我们可以一直这样做,我只是想清楚地说明步骤。编辑在this.FILO上添加了注释-先进,后出。有趣的通常在英语中是后进先出。它的最终含义相同(但与FIFO并行命名-先进先出)。这不是什么大问题,只是好奇而已。