A little problem with Stacks in C

  • Thread starter Thread starter Peon666
  • Start date Start date
AI Thread Summary
The discussion revolves around issues encountered while implementing an RPN calculator in C, specifically related to the stack operations for addition. The user initially faced problems with the `pop` function returning unexpected values, leading to incorrect results during addition. It was identified that the output from `pop` was not the expected integers but rather status codes indicating errors, particularly `ADT_INVALID_STACK`, due to the stack not being properly initialized. After realizing that the `create_stack` function was omitted from the main program, the user included it, which resolved many of the initial issues with stack operations. The addition of integers now functions correctly, and the user is looking to expand the stack's capacity dynamically using `alloc()` and `realloc()`. The discussion highlights the importance of proper stack initialization and the need for debugging techniques to identify issues in code. The user seeks guidance on implementing dynamic memory allocation for the stack in the `create_stack` function or the main program.
Peon666
Messages
107
Reaction score
0
Alright guys, I was implementing RPN calculator in C language and during that, I have a little problem with the summing and poping thing. Here's how it goes:

My code is here:

case '+':
if (i < 2)
printf ("Not enough operators to perform addition operation!\n");
array2[0] = pop(mystack, &array[0]); // Output: 1
array2[1] = pop(mystack, &array[1]); // Output: 1
printf ("%d\n", array[0]); // Output: 3 (as I entered)
printf ("%d\n", array[1]); // Output: 4 (as I entered)
temp = (array[0]+array[1]);
push (mystack, temp);
printf ("%d\n", temp); // Output: 1
temp2 = pop (mystack, &temp);
printf ("%d\n",temp2); // Output: 1
break;

Now the problem is that this does not add the two integers. Why is this happening? Besides, it does not take anything in array2[0] and array2[1]. When I pop the elements array[0] and array[1] into array2[0] and array2[1] and print them out, only 1 is printed out. What could be the problem?

Further, when I add array[0] and array[1] into temp and print temp out, again, 1 is printed and nothing else. However, if I print out array[0] and array[1] separately, they are displayed as I enter them, i.e 3 and 4. What's going on? I'm really troubled about this fiasco! :(
 
Technology news on Phys.org
Show pop & push.
 
Instead of asking us to debug your code (and you have not shown push or pop), use the debugger. Learn how to use the debugger. It will help you understand why your code works the way it does, which is often quite different from how you think it should work.
 
Here are the push and pop functions:

Code:
int push (stackADT stack, stack_elem_t elem)
{
    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    if (stack->stack_ptr == (MAX_STACK_DEPTH - 1)) {
        return (ADT_STACK_FULL);
    }
    ++stack->stack_ptr;
    stack->stk[stack->stack_ptr] = elem;
    return (ADT_NOERROR);
}

int pop (stackADT stack, stack_elem_t *pelem)
{
    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    if (stack->stack_ptr == -1) {
        return (ADT_STACK_EMPTY);
    }
    *pelem = stack->stk[stack->stack_ptr];
    --stack->stack_ptr;
    return (ADT_NOERROR);
}

What now? And I do have problems dealing with the debugger. :(
 
Those array2 values you obtained when calling pop are not the values from the stack. They are the status from calling pop: One of ADT_INVALID_STACK, ADT_STACK_FULL, and ADT_NOERROR. What are these enum values?

I suspect that ADT_NOERROR is zero and the others are non-zero. If that is the case, your array2 values indicate that pop did not work. Something is wrong with your stack.
 
What could be that something? Should I post other piece of the code as well?
 
I'll post my entire code. See what and where the problem exists:

(I'm posting it all other than the MAIN programme which I've already posted)

Code:
#include <stdio.h>
#include <stdlib.h>

#define  MAX_STACK_DEPTH  10

enum stackADT_enums {
    ADT_NOERROR,
    ADT_INVALID_STACK,
    ADT_NOMEM,
    ADT_STACK_FULL,
    ADT_STACK_EMPTY,
};


typedef int stack_elem_t;
typedef struct stackCDT *stackADT;

struct stackCDT {
    stack_elem_t stk[MAX_STACK_DEPTH];
    int stack_ptr;
};

int create_stack (stackADT *pstack);
int free_stack (stackADT stack);
int push (stackADT stack, stack_elem_t elem);
int pop (stackADT stack, stack_elem_t *pelem);
int depth (stackADT stack);
int print_stack (stackADT stack);


int create_stack (stackADT *pstack)
{
    *pstack = malloc(sizeof(struct stackCDT));
    if (!*pstack) {
        return (ADT_NOMEM);
    }
    (*pstack)->stack_ptr = -1;
    return (ADT_NOERROR);
}

int free_stack (stackADT stack)
{
    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    free(stack);
    return (ADT_NOERROR);
}

int push (stackADT stack, stack_elem_t elem)
{
    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    if (stack->stack_ptr == (MAX_STACK_DEPTH - 1)) {
        return (ADT_STACK_FULL);
    }
    ++stack->stack_ptr;
    stack->stk[stack->stack_ptr] = elem;
    return (ADT_NOERROR);
}

int pop (stackADT stack, stack_elem_t *pelem)
{
    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    if (stack->stack_ptr == -1) {
        return (ADT_STACK_EMPTY);
    }
    *pelem = stack->stk[stack->stack_ptr];
    --stack->stack_ptr;
    return (ADT_NOERROR);
}

int print_stack (stackADT stack)
{
    int i;

    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    printf("Top ");
    for (i = stack->stack_ptr ; i >= 0 ; --i) {
        printf("%d, ", stack->stk[i]);
    }
    printf("Bottom\n");
    return (ADT_NOERROR);
}

void print_menu (void)
{
    printf("\nPress:\n");
    printf("1 to create stack.\n");
    printf("2 to free stack.\n");
    printf("3 to push element.\n");
    printf("4 to pop element.\n");
    printf("5 to print stack contents.\n");

    return;
}

What now?
 
You have a stack error. Look at your own results:

array2[0] = pop(mystack, &array[0]); // Output: 1

That output value of one means you had an ADT_INVALID_STACK error.
 
I just figured I didn't even include the create_stack thing in the main programe. That was pretty dumb, I know. Let me see if it works well after creating a stack. Hopefully it would.
 
  • #10
It looks like you're missing a lot of pointers
 
  • #11
Peon666: Try the following. Let us know what you get for the third and fourth outputs, below. If you get 7 for the third output, but 1 for the fourth output, wouldn't that mean push is causing a problem (probably because you omitted create_stack, as you mentioned)?

printf("%d\n",array[0]); // output: 3 (as I entered)
printf("%d\n",array[1]); // output: 4 (as I entered)
temp=array[0]+array[1];
printf("temp=%d\n",temp); // output: temp=
push(mystack,temp);
printf("temp=%d\n",temp); // output: temp=
 
  • #12
Alright, after including the create_stack in the main code, the problem has been reduced considerably. Push and Pop is working pretty fine, so are the operations of addition, subtraction etc.

Now, I have a further query. I want to use alloc() and then realloc() in the code. (Supposing that we initially enter five elements and then we decide for more elements so we use realloc()).

How could this be done? and would this be done in the main programe or in the fuction create_stack?
 
Back
Top