使用栈“推”和“流行” [英] using “push” and “pop” in a stack

查看:198
本文介绍了使用栈“推”和“流行”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求我填写一份堆栈随机变量和流行出来的FILO秩序的任务。虽然我设法得到它填补了栈,它似乎是突然跳出最后一个元素而已。我不知道为什么。任何帮助将是AP preciated。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;
#定义STACK_SIZE 10
#定义STACK_EMPTY -1
无效的push(的char [],//输入/输出中 - 堆栈
          字符,输入// - 数据被压入堆栈
          为int *,//输入/输出 - 指针堆栈的顶部的索引
          INT); //恒 - 堆栈的最大尺寸
焦炭产量// - 数据从堆栈中被弹出了
POP(的char [],//输入/输出 - 堆栈
    为int *); //输入/输出 - 指针堆栈的顶部的索引
无效的push(炭堆[],焦炭项目,为int *顶部,诠释MAX_SIZE){
    堆叠[*顶部++] =项目;
}
焦炭POP(炭堆[],INT *顶部){
    返回栈[* top--]
}
诠释主(){
    个char [STACK_SIZE]
    INT s_top = STACK_EMPTY; //指针指向堆栈的顶部的索引    焦炭randChar ='';
    INT I = 0;
    INT J = 0;
    INT randNum = 0;    函数srand(时间(NULL));    对于(i = 0; I< STACK_SIZE;我++){
        randNum = 33 +(INT)(兰特()%((126-33)+ 1));
        randChar =(char)的randNum;
        推(S,randChar,&安培; s_top,STACK_SIZE);        的printf(随机字符:%C \\ N,randChar);    }
    的printf(----------- \\ n);    为(J = STACK_SIZE; J&大于0;的J - ){
        的printf(随机字符:%C \\ N,流行(S,放大器; s_top));
    }
    返回0;
}


解决方案

您推应

 (*顶部)++;
堆叠[*顶] =价值;

这是第一个增量到下一个空的位置,然后插入。在变量总是指向栈顶元素。因此推,第一个增量然后分配。流行,第一次提取顶部的值,然后递减。

注:以上线可以乱棍堆叠[++(*顶部)] =值

在目前的code,在先推的,你的code。与堆叠[*顶部++] =项目,与后增量试图将值分配给当前值 *顶部 1 键,然后增加,这是不对的。

对于推动常规的这一修改弹出程序是好的。

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.

#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;
}

解决方案

Your push should be

(*top)++;
stack[*top] = value;

That is first increment to the next empty position and then insert. The top variable always points to the top element. Therefore to push, first increment then assign. To pop, first extract the value at top and then decrement.

Note: the above line can be clubbed to 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 *top which is -1 and then increment, which is wrong.

With respect to this modification of push routine the pop routine is okay.

这篇关于使用栈“推”和“流行”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆