A little problem with Stacks in C

  • Thread starter Peon666
  • Start date
In summary, after fixing a stack error and including the create_stack function in the main program, the RPN calculator in C language is now functioning properly with addition, subtraction, and other operations. The user also has the option to dynamically allocate more elements using alloc() and realloc().
  • #1
Peon666
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! :(
 
Technology news on Phys.org
  • #2
Show pop & push.
 
  • #3
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
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
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
What could be that something? Should I post other piece of the code as well?
 
  • #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:
#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
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
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?
 

1. What is a stack in C?

A stack in C is a data structure that follows the Last In First Out (LIFO) principle. This means that the last item added to the stack will be the first one to be removed. It is similar to a stack of books, where the last book placed on top is the first one to be taken off.

2. How do you declare a stack in C?

To declare a stack in C, you can use an array or a linked list. For an array implementation, you will need to specify the size of the stack and use the push and pop operations to add and remove items. For a linked list implementation, you will need to create a struct for each node and use pointers to keep track of the top of the stack.

3. What are the main operations performed on a stack?

The main operations performed on a stack are push, pop, and peek. Push is used to add an item to the top of the stack, pop is used to remove the top item from the stack, and peek is used to view the top item without removing it.

4. What is the time complexity of stack operations in C?

The time complexity of push, pop, and peek operations on a stack in C is O(1). This means that the time it takes to perform these operations is constant and does not depend on the size of the stack.

5. What are some common applications of stacks in C?

Stacks are commonly used in programming for tasks such as implementing function calls, browser history, and undo/redo functionality. They can also be used in algorithms such as depth-first search and backtracking. Additionally, stacks are used in memory management and expression evaluation.

Similar threads

  • Programming and Computer Science
Replies
2
Views
932
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
735
Back
Top