#include typedef struct node { int val; struct node *next; } NODE; typedef struct { NODE *head; int size; } STACK; /* * Creates an empty stack */ void MakeEmptyStack(STACK *stack) { printf("Stack Initialized!\n"); stack->head = NULL; stack->size = 0; } /* * Return the boolean value for whether the stack is * empty or not */ int IsEmptyStack(STACK *stack) { if (stack->head == NULL) printf("Stack Empty!\n"); else printf("Stack has %d items!\n", stack->size); return (stack->head == NULL); } /* * Display head node information */ void top(STACK *stack) { if (IsEmptyStack(stack)) return; printf("\n===============================\n"); printf("Printing Stack Head\n"); printf("===============================\n"); printf("Value: %d\n", (stack->head)->val); printf("Next Address: %d\n", (stack->head)->next); printf("===============================\n"); } /* * Insert Value into head position */ void push(int value, STACK *stack) { NODE *node; // allocate space node = (NODE *) malloc (sizeof(NODE)); // assign values node->val = value; node->next = stack->head; // point head to this node stack->head = node; // increase stack size (stack->size)++; } /* * Delete head node */ void pop(STACK *stack) { NODE *p; if (IsEmptyStack(stack)) return; printf("%d\n", (stack->head)->val); p = stack->head; stack->head = (stack->head)->next; (stack->size)--; free(p); } int main() { STACK *stack; int i; stack = (STACK *)malloc(sizeof(STACK)); MakeEmptyStack(stack); IsEmptyStack(stack); for(i=0; i<10; i++) { push(i, stack); } top(stack); IsEmptyStack(stack); for(i=0; i<10; i++) { pop(stack); } }