// stack with integer values #include "istack.h" // The stack is implemented by having a node for the stack // which points to the first real item of the stack. // Initialize the stack as an empty stack void stackInitialize(StackItem * stack) { stack->next = NULL; // Initialize as empty // Note we don't care about data value in the control node } // Add the value to top of stack void push(StackItem * stack, int n) { StackItem *tmp = (StackItem *)(malloc (sizeof (struct StackItem))); tmp->value = n; tmp->next = stack->next; stack->next = tmp; } // Check if stack is empty int stackIsEmpty(StackItem * stack) { return stack->next == NULL ? 1 : 0; } // Remove and return the top value int pop(StackItem * stack) { int retval = 0; if (!stackIsEmpty(stack)) { StackItem * tmp = stack->next; stack->next = stack->next->next; retval = tmp->value; free(tmp); } return retval; } // Display the stack void stackDisplay(StackItem * stack) { for ( StackItem * current = stack->next; current != NULL; current = current->next ) { if (current != stack->next) printf(" "); printf("%d", current->value); } printf("\n"); }