/* CSE 303, Spring 2009, Marty Stepp This program creates a simple stack of ints and provides a text UI for pushing, popping, and clearing the stack. This program is intended to practice manipulating linked data structures in C. */ #include #include /* A struct type to represent individual nodes in a linked stack. */ typedef struct Node { int data; struct Node* next; } Node; int main(void) { int choice; Node* stack; // pointer to top of stack while (1) { printf("(1) push, (2) pop, (3) clear, or (0) quit? "); scanf("%d", &choice); if (choice == 1) { // push Node* oldFront = stack; stack = (Node*) malloc(sizeof(Node)); printf("Number to push? "); scanf("%d", &stack->data); stack->next = oldFront; } else if (choice == 2) { // pop if (stack) { Node* oldFront = stack; printf("%d\n", stack->data); stack = stack->next; free(oldFront); } } else if (choice == 3) { // clear while (stack) { Node* oldFront = stack; stack = stack->next; free(oldFront); } } else { // quit break; } } return 0; }