#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void push(char );
char pop(void);
void dump(void);
char array[10];
int pos=0;
int main(void)
{
char input,output;
int option;
char user;
do
{
printf("使用者現在想要做(1)push或是(2)pop?\n");
scanf("%d",&option);
if(option==1)
{
printf("請填入輸入字元:\n");
scanf("%c",&input);
//input=getch();
push(input);
dump();
}
else
{
output=pop();
printf("pop取出%c\n",output);
dump();
}
printf("要繼續(y/n)?\n");
scanf("%c",&user);
//user=getch();
}while(user!='n');
return 0;
}
void push(char a)
{
if(pos<10)
{
array[pos++]=a;
}
else
printf("堆疊滿了\n");
}
char pop(void)
{
if(pos<=0)
{
printf("堆疊空了無法pop\n");
return '\0';
}
else
return array[--pos];
}
void dump(void)
{
int i;
if(pos>0)
{
printf("堆疊裡有: ");
for(i=pos-1;i>=0;i--)
printf(" %c",array[i]);
}
else
printf("堆疊空無一物\n");
}