#include #include #include #define MAX 100 int stack[MAX]; int top = -1; int main() { char option; int i ; while(1){ printf("\n*****************************\n"); printf(" <1>insert(push)\n"); printf(" <2>delete(pop)\n"); printf(" <3>list\n"); printf(" <4>quit\n"); printf("\n*****************************\n"); printf("Please enter your choice: "); option = getche(); switch(option){ case'1': if (top >= MAX-1) /* 當堆疊已滿,顯示錯誤 */ printf("Stack is full !"); else { top++; printf("\n\n Enter item to insert:"); scanf( "%d", &stack[top]); } break; case'2': if (top < 0) /* 當堆疊沒有資料存在,顯示錯誤 */ printf("\n No item, stack is empty !\n"); else { printf("\n\n Item %d deleted\n", stack[top]); top --; } break; case'3': printf("\n目前堆疊的數字為: "); for(i=0;i<=top;i++){ printf("%d ",stack[i]); } printf("\n"); break; case'4': return 1; break; } } return 0; }