c语言数据结构问题
#include<stdio.h>#include<stdlib.h>#definemaxsize10typedefstructNode{intdata[maxsize]...
#include<stdio.h>
#include<stdlib.h>
#define maxsize 10
typedef struct Node
{
int data[maxsize];
int top;
}SqStack;
SqStack *CreateStack()
{
SqStack *st;
st->top = -1;
printf("请输入栈元素:\n");
for(;st->top < maxsize;)
{
st->top++;
scanf("%d",&st->data[st->top]);
}
return st;
}
void PrintStack(SqStack *st)
{
st->top = maxsize-1;
printf("该栈元素为:\n");
for(;st->top >= 0;st->top--)
{
printf("%d",st->data[st->top]);
}
}
main()
{
SqStack *st;
st = CreateStack();
PrintStack(st);
} 展开
#include<stdlib.h>
#define maxsize 10
typedef struct Node
{
int data[maxsize];
int top;
}SqStack;
SqStack *CreateStack()
{
SqStack *st;
st->top = -1;
printf("请输入栈元素:\n");
for(;st->top < maxsize;)
{
st->top++;
scanf("%d",&st->data[st->top]);
}
return st;
}
void PrintStack(SqStack *st)
{
st->top = maxsize-1;
printf("该栈元素为:\n");
for(;st->top >= 0;st->top--)
{
printf("%d",st->data[st->top]);
}
}
main()
{
SqStack *st;
st = CreateStack();
PrintStack(st);
} 展开
1个回答
展开全部
createStack中
赋值前应该先给st分配内存
可以
st=malloc(sizeof (*st));
这样
赋值前应该先给st分配内存
可以
st=malloc(sizeof (*st));
这样
追问
输出不了
追答
#include<stdio.h>
#include<stdlib.h>
#define maxsize 10
typedef struct Node
{
int data[maxsize];
int top;
}SqStack;
SqStack *CreateStack()
{
SqStack *st = malloc(sizeof(*st));
st->top = -1;
printf("请输入栈元素:\n");
for(;st->top < maxsize;)
{
st->top++;
scanf("%d",&st->data[st->top]);
}
return st;
}
void PrintStack(SqStack *st)
{
st->top = maxsize-1;
printf("该栈元素为:\n");
for(;st->top >= 0;st->top--)
{
printf("%d",st->data[st->top]);
}
}
main()
{
SqStack *st;
st = CreateStack();
PrintStack(st);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询