#include #include "stack.h" #define BUF_SIZE 1024 void instructions() { printf("To use the stack\n"); printf("1: push a value onto stack\n"); printf("2: pops from stack\n"); printf("3: prints content of stack\n"); printf("4: exits\n"); } int main() { Node *stack = NULL; int choice; int value; char buffer[BUF_SIZE]; // Print the instructions instructions(); // Read the user's choice printf("> "); fgets(buffer,BUF_SIZE,stdin); buffer[strlen(buffer)-1] = '\0'; choice = atoi(buffer); // We could also use scanf as shown below // but in hw4, it is easier to read the user's input // with fgets, so we demonstrate it here //scanf("%d",&choice); while ( choice != 4 ) { switch ( choice ) { case 1: printf("Enter int: "); fgets(buffer,BUF_SIZE,stdin); buffer[strlen(buffer)-1] = '\0'; value = atoi(buffer); // Once again, we could have used scanf instead //scanf("%d",&value); spush(&stack,value); break; case 2: if ( sis_empty(stack) ) { printf("Empty\n"); } else { value = spop(&stack); printf("Popped: %d\n",value); } break; case 3: sprint(stack); break; default: instructions(); break; } printf("> "); fgets(buffer,BUF_SIZE,stdin); buffer[strlen(buffer)-1] = '\0'; choice = atoi(buffer); //scanf("%d",&choice); } return 0; }