标题:进栈元素怎么输不进去啊????
取消只看楼主
Nield
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-4-23
结帖率:0
 问题点数:0 回复次数:0 
进栈元素怎么输不进去啊????
//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);
    }
}
搜索更多相关主题的帖子: next 元素 copyright include choice 
2013-05-06 22:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-406373-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.175347 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved