A little problem with Stacks in C

  • Thread starter Peon666
  • Start date
  • #1
108
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! :(
 

Answers and Replies

  • #2
Borek
Mentor
28,530
2,963
Show pop & push.
 
  • #3
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
685
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.
 
  • #4
108
0
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. :(
 
  • #5
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
685
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.
 
  • #6
108
0
What could be that something? Should I post other piece of the code as well?
 
  • #7
108
0
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?
 
  • #8
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
685
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.
 
  • #9
108
0
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
490
2
It looks like you're missing a lot of pointers
 
  • #11
nvn
Science Advisor
Homework Helper
2,128
32
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
108
0
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?
 

Related Threads on A little problem with Stacks in C

  • Last Post
Replies
10
Views
3K
Replies
2
Views
1K
  • Last Post
Replies
4
Views
1K
Replies
1
Views
1K
  • Last Post
Replies
7
Views
2K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
7
Views
9K
  • Last Post
Replies
3
Views
2K
Replies
5
Views
1K
  • Last Post
Replies
3
Views
1K
Top