1
votes

I'm trying to figure out why my stack struct is not popping the elements and considers the stack to be NULL (i get the else condition from the pop() executing both times)? I'm confused because the printf shows the elements are being added onto to the stack.

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

typedef struct node {
        int element;
        struct node *pnext;
} node_t;

void push(node_t *stack, int elem){
        node_t *new = (node_t*) malloc(sizeof(node_t)); // allocate pointer to new node memory 
        if (new == NULL) perror("Out of memory");
        new->pnext = stack; 
        new->element = elem;
        stack = new; // moves the stack back to the top 
        printf("%d\n", stack->element);
}

int pop(node_t *stack) {
        if (stack != NULL) {
                node_t *pelem = stack;
                int elem = stack->element;
                stack = pelem->pnext; // move the stack down 
                free(pelem);  // free the pointer to the popped element memory 
                return elem;
        }
        else {
                printf("fail");
                return 0; // or some other special value
        }
}

int main(int argc, char *argv[]){
        node_t *stack = NULL ; // start stack as null 
        push(stack, 3);
        push(stack, 5);
        int p1 = pop(stack);
        int p2 = pop(stack);
        printf("Popped elements: %d %d\n", p1, p2);
        return 0 ;
}
1
Does this answer your question? Push of stack not inserting the new value - C - walnut
Just look at main(). 'stack' is initialized to NULL. Nothing after that changes, or can change, its value. - Martin James

1 Answers

1
votes

As said in a remark when you exit push/pop the variable stack in main is unchanged, so it is like you did nothing, except a memory leak in push

To have the new stack in main after a push that function can return the new stack, but this is not possible for pop already returning the poped value, so to have the same solution for both just use give the address of the variable in parameter to the functions to allow to modify it, so a double pointer rather than a simple

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

typedef struct node {
        int element;
        struct node *pnext;
} node_t;

void push(node_t ** stack, int elem){
        node_t *new = (node_t*) malloc(sizeof(node_t)); // allocate pointer to new node memory 
        if (new == NULL) {
          perror("Out of memory");
          exit(-1);
        }
        new->pnext = *stack; 
        new->element = elem;
        *stack = new; // moves the stack back to the top 
        printf("%d\n", (*stack)->element);
}

int pop(node_t ** stack) {
        if (*stack != NULL) {
                node_t *pelem = *stack;
                int elem = (*stack)->element;
                *stack = pelem->pnext; // move the stack down 
                free(pelem);  // free the pointer to the popped element memory 
                return elem;
        }
        else {
                printf("fail");
                return 0; // or some other special value
        }
}

int main(int argc, char *argv[]){
        node_t *stack = NULL ; // start stack as null 
        push(&stack, 3);
        push(&stack, 5);
        int p1 = pop(&stack);
        int p2 = pop(&stack);
        printf("Popped elements: %d %d\n", p1, p2);
        return 0 ;
}

Compilation and execution :

% gcc -Wall s.c
% ./a.out
3
5
Popped elements: 5 3