7

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;
}
walkirie27
  • 73
  • 1
  • 2
  • 6
  • 3
    FILO — First In, Last Out. Intriguing! Normally in English, it is LIFO — Last In, First Out. It ends up meaning the same (but is named in parallel with FIFO — First In, First Out). Not a huge problem — just curiosity. – Jonathan Leffler Sep 12 '13 at 06:11

3 Answers3

6

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.

phoxis
  • 60,131
  • 14
  • 81
  • 117
0

I'll mix both the answers (one got deleted just now ) :

You've have to fix both push and 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

See only push updated and push and pop both updated

P0W
  • 46,614
  • 9
  • 72
  • 119
0

Postfix ++ and -- have higher precedence than unary *, so in order to increment the thing that top points to, you need to write (*top)++ and (*top)--; *top++ will advance the pointer, which is not what you want.

Secondly, the stack pointer should always point to the last thing added to the stack, so you want to increment the stack pointer before writing 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 ++*top is interpreted as ++(*top), but the parens help make things clear.

Push and pop should always be the inverse of each other; if you push with ++(*top), you need to pop with (*top)--.

John Bode
  • 119,563
  • 19
  • 122
  • 198