进栈元素怎么输不进去啊????
//All copyright are preserved bycobby/*进栈与出栈*/
#include<stdio.h>
#include<malloc.h>
#define NULL 0
typedef struct node
{
char ch;
struct node *next;
}Snode,*stack;
void main()
{
stack s,top,p,q;
char ch;
int choice;
s=(stack)malloc(sizeof(Snode));
s->ch='!';
s->next=NULL;
top=s;
printf("请输入进栈元素:\n");
scanf("%c",&ch);
getchar();
while(ch!='!')
{
p=(stack)malloc(sizeof(Snode));
p->ch=ch;
p->next=top;
top=p;
scanf("%c",&ch);
getchar();
}
printf("从栈顶到栈底的元素是:");
while(p->next!=NULL)
{
printf("%5c",p->ch);
p=p->next;
}
printf("\n");
printf("输入“1”为进栈,输入“2”为出栈,其他则退出!!!\n");
scanf("%d",&choice);
while(choice==1||choice==2)
{
if(choice==1)//进栈
{
printf("请输入进栈元素:");
p=(stack)malloc(sizeof(Snode));
scanf("%c",&p->ch);
p->next=top;
top=p;
}
//出栈
else if(choice==2)
{
if(top->next!=NULL)
{
q=top->next;
free(top);
top=q;
}
else
{
printf("栈已空\n");
}
}
else
{
exit(0);
}
printf("从栈顶到栈底的元素是:");
while(top->next!=NULL)
{
printf("%5c",top->ch);
top=top->next;
}
printf("输入“1”为进栈,输入“2”为出栈,其他则退出!!!\n");
scanf("%d",&choice);
}
}