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;
}
把你的报错异常贴出来看看
你是不是没学过指针啊, 都没见你代码里出现过*
...你对着看一下
#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;
}
谢谢大佬