#include "stack.h" void push(Node **top, int value) { Node *new_element = (Node*)malloc(sizeof(Node)); if ( !new_element) { fprintf(stderr,"Out of memory\n"); return; } new_element->next = *top; new_element->value = value; *top = new_element; } // Return the popped value or -1 if the stack is empty int pop(Node **top) { if ( is_empty(*top) ) { return -1; } Node *temporary_top = *top; int value = (*top)->value; *top = temporary_top->next; //*top = (*temporary_top).next; free(temporary_top); temporary_top = NULL; return value; } bool is_empty(Node *top) { return (top == NULL); } void print(Node *top) { Node *current = top; while ( current != NULL ) { printf("%d\n",current->value); current = current->next; } } // We could also print the stack as follows /* void print(Node *top) { if ( ! top ) { return; } printf("%d\n",top->value); print(top->next); } */