0

# 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 | 初学一级 | 园豆：186

0

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 | 园豆：1318 (小虾三级) | 2019-10-26 20:15

您需要登录以后才能回答，未注册用户请先注册