P1-GKM+C

$ cat Programming_Assignment_1.c
 * 1) include 
 * 2) include 

int gmax; int gindex;

void push(char *stack) { int n; printf("Input data entry to stack\n"); char str[20]; gets (str); for (n = 0; n <= 19; n++) { stack[n + gindex*20] = str[n]; } gindex ++; }

void show(char* stack) { //show function int n, i; for (n = 0; n < gindex; n++) { printf("\n%i: ",n+1);

for (i = 0; (i <=19) && (stack[i + n*20]!= '\0'); i++) { printf("%c", stack[i + n*20]); } } printf("\n\nThat's all folks.\n"); }

void pop(char* stack) { //pop function int i; printf("\n%i: ",gindex); for (i = 0; (i <=19) && (stack[i + (gindex-1)*20]!= '\0'); i++) { printf("%c", stack[i + (gindex-1)*20]); } gindex--; printf("\n"); }

int main { char *stack; char com[20]; int n = 2, i; gmax = 5; gindex = 0; // temp = (char*) malloc (gmax); stack = (char*) malloc (gmax*20); if (stack == NULL) exit(1); //start of program loop while (n != 1) { //command input printf("Enter 'pop', 'push', 'show', or 'quit' \n"); scanf("%s", &com); //printf("%s\n", com);

//choice selection if (com[2] == 's'){ //expansion of stack fflush(stdin); if (gindex == gmax) { gmax *= 2; stack = (char*) realloc(stack, gmax*20); } //call push push(stack); } else if (com[0] == 'p'){ if (gindex == 0) printf("no data in stack\n");

else pop(stack); } else if (com[0] == 's'){ if (gindex == 0) printf("no data in stack\n");

else show(stack); } else if (com[0] == 'q'){ n = 1; } } free (stack); return 0; }

Remember: we can all improve.