s/* Modified : Michael Robinson Program : stack.c Purpose : stack and memory dump simulator To present push and pop on a stack, and memory dump also show "for" loop as an endeless loop Updated : 03-22-2011 */ #include int main() { int a[10];//size of the stack int i; printf("To pop enter -1 to exit enter 999\n"); for(i = 0;;) //this is an endless loop { printf("Push "); scanf("%d", &a[i]); if(a[i] == 999) //exit "for" loop and dump memory { break; } if( (i > 9) && a[i] != -1 && a[i] != 999 ) { printf(" Stack is FULL, I can not accept %d, you need to pop or exit\n", a[i] ); continue; } if(a[i] == -1) //to pop the stack { if(i == 0) //at the bottom of the stack so it is empty { printf("Underflow\n"); //describes an error nothing to pop } else { printf("pop = %d\n", a[--i]); // } } else { i++; } } //dump memory (stack) printf("\n Memory (stack) Dump\n"); int x; for(x=i-1; x >= 0; x--) { printf(" stack address %d, contains %d\n", x, a[x] ); } }