Data_Structure/作業/unit3/stack.c
2025-01-20 21:30:53 +08:00

55 lines
1.1 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#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;
}