Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A little problem with Stacks in C

  1. Sep 3, 2009 #1
    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:

    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! :(
     
  2. jcsd
  3. Sep 3, 2009 #2

    Borek

    User Avatar

    Staff: Mentor

    Show pop & push.
     
  4. Sep 3, 2009 #3

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  5. Sep 3, 2009 #4
    Here are the push and pop functions:

    Code (Text):


    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. :(
     
  6. Sep 3, 2009 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  7. Sep 3, 2009 #6
    What could be that something? Should I post other piece of the code as well?
     
  8. Sep 3, 2009 #7
    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 (Text):

    #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?
     
  9. Sep 3, 2009 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  10. Sep 3, 2009 #9
    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.
     
  11. Sep 3, 2009 #10
    It looks like you're missing a lot of pointers
     
  12. Sep 3, 2009 #11

    nvn

    User Avatar
    Science Advisor
    Homework Helper

    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=
     
  13. Sep 3, 2009 #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?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: A little problem with Stacks in C
  1. A little C# help. (Replies: 7)

Loading...