Alex
Alex

Reputation: 2299

Stack implemented as an array defaulting first value to 0 in C

I have an assignment where I am supposed to use this very very simple (or so I thought) stack that my teacher wrote in C, just using an array. From this, I have to implement reverse polish notation from a text file.

In order for me to implement this, I am using a stack, pushing values on until I hit an operation. I then do the operation and push the result back onto the stack until the user hits p to print the value.

The problem is, for some reason, my professor's implementation of the stack array defaults the first (index 0) value to 0. Printing the stack without pushing anything onto it should result in null but it appears the output is 0.

Here is my professor's implementation of the stack:

#define STK_MAX 1024
#define ELE int

ELE _stk[STK_MAX];
int _top = 0;

void stk_error(char *msg)
{
    fprintf(stderr, "Error: %s\n", msg);
    exit(-1);
}

int stk_is_full()
{
    return _top >= STK_MAX;
}

int stk_is_empty()
{
    return _top == 0;
}

void stk_push(ELE v)
{
    if ( stk_is_full() )
        stk_error("Push on full stack");
    _stk[_top++] = v;
}

ELE stk_pop()
{
    if ( stk_is_empty() )
        stk_error("pop on empty stack");
    return _stk[--_top];
}

void print()
{
    for(int i = 0; i <= _top; ++i)
         printf("%d ", _stk[i]);
    printf("\n");
}

I realize that the print statement will print a value that has not been pushed yet, but the problem is, is that when I don't print it, it still ends up there and it ends up screwing up my rpn calculator. Here is what happens when I do this:

// input
stk_push(2);
print();
stk_push(4);
print();
// output
2 0
2 4 0

How do I get rid of the 0 value that is affecting my calculator? Doing stk_pop() after the pushing the first value onto the stack didn't seem to work, and checking that top == 0, then directly inserting that element before incrementing _top didn't work.

Upvotes: 0

Views: 75

Answers (2)

AaronLS
AaronLS

Reputation: 38365

"The problem is, is that the rpn calculator relies on the TOS being accurate. When I do pop() though, it will pop 0 and not the real TOS."

Sounds like a problem with your calculator implementation. You assumed the top of the stack would be null, but that's not the case for your professors stack implementation. Simply a invalid assumption.

Instead he's provided a stk_is_empty() method to help determine when you've pop everything.

If you need to pop all elements, you'll need to break on the condition of stk_is_empty().

stk_push(2);
stk_push(4);

while( stk_is_empty() == false)
{
   stk_pop();
}

Of course in reality you'd be setting the pop return to a variable and doing something with it. The key point is leveraging stk_is_empty().

I haven't written C++ in few years so hopefully I didn't make a minor syntax error.

Upvotes: 0

a_pradhan
a_pradhan

Reputation: 3295

When you are printing, loop from 0 to (_top - 1), since your top most element is actually at _top - 1. Hint : Look at your pop/push method.

void print()
{
    for(int i = 0; i < _top; ++i)
         printf("%d ", _stk[i]);
    printf("\n");
}

Upvotes: 3

Related Questions