注册 登录
编程论坛 数据结构与算法

关于用顺序栈反序输出字符串,弄不好啊,求大神。。

huchao1hao 发布于 2013-04-01 22:15, 1118 次点击
#include <stdio.h>
#define Stack_Size 50
typedef struct
{
    char elem[Stack_Size];
    int top;
}SeqStack;

void InitStack(SeqStack *s)
{
    s->top=-1;
}
int Push(SeqStack *s, char x)
{
    if(s->top==Stack_Size-1)
        return(0);
    s->top++;
    s->elem[s->top]=x;
    return(1);
}
int Pop(SeqStack *s, char *x)
{
    do
    {    *x=s->elem[s->top];
        printf("%s",*x);
        s->top--;
        return(1);
    }while(s->top==-1);
}

main()
{
    SeqStack *l;
    char x,y;
    l=(SeqStack *)malloc(sizeof(SeqStack));
    InitStack(l);;
    do
    {   
        scanf("%c",&x);
        Push(l,x);
    }while(x='\n');
    Pop(l,y);
}




纯新手,对着书写的,书上只有一段段,但是自己组合就弄不好,拜托大神看看哪里错了。。
8 回复
#2
不玩虚的2013-04-01 22:50
那有错
#3
huchao1hao2013-04-01 22:59
字符打进去按回车没有反序字符串输出额。。
#4
huchao1hao2013-04-02 17:38
没有人了木。
#5
azzbcc2013-04-02 17:43
x='\n',啧啧,细心那,改成 x == '\n'
#6
huchao1hao2013-04-02 21:25
按楼上的说的,试了下输入字符串后提示执行错误额。。还是结构上有什么错误额,我太蠢了。。

[ 本帖最后由 huchao1hao 于 2013-4-2 21:26 编辑 ]
#7
azzbcc2013-04-02 21:38
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define Stack_Size 50
typedef struct
{
    char elem[Stack_Size];
    int top;
}SeqStack;

void InitStack(SeqStack *s)
{
    s->top=-1;
}
int Push(SeqStack *s, char x)
{
    if(s->top==Stack_Size-1)
        return(0);
    s->top++;
    s->elem[s->top]=x;
    return(1);
}
int Pop(SeqStack *s, char *x)
{
    do
    {    *x=s->elem[s->top];
    printf("%c",*x);    //这里是粗心吧
    s->top--;
    }while(s->top!=-1);    //这里改成不等于
   
    return(1);
}

int main()
{
    SeqStack *l;
    char x,y;
    l=(SeqStack *)malloc(sizeof(SeqStack));
    InitStack(l);;
    do
    {   
        scanf("%c",&x);
        Push(l,x);
    }while(x != '\n');        //当时没细看,你应该是这个意思
    Pop(l,&y);
    return 0;
}
#8
azzbcc2013-04-02 21:45
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define Stack_Size 50
typedef struct
{
    char elem[Stack_Size];
    int top;
}SeqStack;

void InitStack(SeqStack *s)
{
    s->top=-1;
}

int push(SeqStack *s, char e)
{
    if (s->top >= Stack_Size-1)
    {
        return 1;
    }
    s->elem[++s->top] = e;

    return 0;
}
int pop(SeqStack *s, char *e)
{
    if (s->top == -1)
    {
        return 1;
    }
    *e = s->elem[s->top--];
    return 0;
}

int main()
{
    char x, y;
    SeqStack *l;

    l=(SeqStack *)malloc(sizeof(SeqStack));
    InitStack(l);

    do
    {   
        x = getchar();
        push(l, x);
    }while(x != '\n');
    pop(l, &y);    //弹出栈顶的回车符

    while (l->top != -1)
    {
        pop(l, &y);
        putchar(y);
    }
    puts("");
    return 0;
}


[ 本帖最后由 azzbcc 于 2013-4-2 21:46 编辑 ]
#9
huchao1hao2013-04-02 22:23
感谢大神
1