标题:进栈元素怎么输不进去啊????
只看楼主
Nield
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-4-23
结帖率:0
 问题点数:0 回复次数:3 
进栈元素怎么输不进去啊????
//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
小王子的花
Rank: 1
等 级:新手上路
帖 子:24
专家分:9
注 册:2013-4-24
得分:0 
感觉你好像没有初始化一个栈,呵呵,我也是新手,说错见谅哈
2013-05-08 10:28
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6809
专家分:42393
注 册:2010-12-16
得分:0 
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);
     }

红色的代码
你不断的在改变top,哪有这样维持一个链表的?  严重的逻辑错误

我行我乐
我的博客:
http://blog.yuccn. net
2013-05-08 12:22
凤儿飘飘
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-4-11
得分:0 
回复 3楼 yuccn
它怎么说exit没有定义啊
2015-04-12 13:16



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




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

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