首页 新闻 会员 周边 捐助

正在学栈,哪位大佬能告诉我我这个程序哪里出错了?

0
悬赏园豆:5 [待解决问题]

include<stdio.h>

include<stdlib.h>

typedef struct sequenstack{
int M;
char data;
int top;
}
Stack;
Stack inti_sequenstack()
{
Stack S;
S=(Stack)malloc(sizeof(struct sequenstack));
S->data=(char)malloc(Msizeof(char));
S->top=-1;
S->M=M;
return S;
}
int push(Stack S,char x)
{
if(S->top=S->M)
return 0;
S->top++;
S->data[S->top]=x;
return 1;
}
int pop(Stack S)
{
if(S->top==-1)
return 0;
S->top--;
return 1;
}
int main()
{
int M;
char c;
Stack S;
S=inti_sequenstack(M);
scanf("%c",&c);
push(S,c);
printf("%c",S->data[S->top]);
return 0;
}

axaxaxya的主页 axaxaxya | 初学一级 | 园豆:186
提问于:2019-10-26 08:54
< >
分享
所有回答(2)
0

把你的报错异常贴出来看看

雙孖星 | 园豆:146 (初学一级) | 2019-10-26 10:18
0

你是不是没学过指针啊, 都没见你代码里出现过*...你对着看一下

#include<stdio.h>
#include<stdlib.h>
const int M=100; //3
typedef struct sequenstack{
    int M;
    char *data; // 4,*
    int top;
}*Stack; // 1, 少了一个*
Stack inti_sequenstack()
{
    Stack S;
    S=(Stack)malloc(sizeof(struct sequenstack));
    S->data=(char*)malloc(M*sizeof(char)); //2, 少了两个*
    S->top=-1;
    S->M=M;
    return S;
}
int push(Stack S,char x)
{
    if(S->top==S->M) // 5,判断写成了赋值
        return 0;
    S->top++;
    S->data[S->top]=x;
    return 1;
}
int pop(Stack S)
{
    if(S->top==-1)
        return 0;
    S->top--;
    return 1;
}
int main()
{
    int M;
    char c;
    Stack S;
    S=inti_sequenstack(M);
    /* scanf("%c",&c); */
    /* push(S,c); */
    /* printf("%c",S->data[S->top]); */
    // 测试
    for(int i = 'a'; i < 'z'; i++)
        push(S,i);
    for(int i = 0 ; i < 26 ;i++){
        printf("%c",S->data[S->top]);
        pop(S);
    }
    puts("");
    printf("pop(S) = %d\n", pop(S));
    return 0;
}
jakio6 | 园豆:1325 (小虾三级) | 2019-10-26 20:15

谢谢大佬

支持(0) 反对(0) axaxaxya | 园豆:186 (初学一级) | 2019-10-26 20:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册