问题见红字
#include<stdio.h>
#include <stdlib.h>
const int StackSize=100; //顺序栈的初始分配空间
typedef int ElemType; // 数据元素类型
typedef struct sq
{ ElemType data[StackSize]; // 一维数组子域
int top; // 栈顶指针子域
}SqStack; // 栈的顺序结构体类型
//函数声明
void InitStack(SqStack *&sq);
int Push(SqStack *sq,ElemType x);
void out_s(SqStack sq);
//函数实现
void InitStack(SqStack *&sq) //初始化栈
{
sq=(SqStack *)malloc(sizeof(SqStack));
sq->top=-1;
}
int Push(SqStack *sq,ElemType x) //进栈
{
if(sq->top==StackSize-1) return 0; //栈满
else
{
sq->top++;
sq->data[sq->top]=x;
return 1;
}
}
void out_s(SqStack *sq)
{
int i;
printf("\n");
if(sq->top<0)
{
printf("这是一个空栈 !");
printf("\n");
}
for(i=sq->top;i>=0;i--)
printf("第 %d 个数据元素是: %d\n",i,sq->data[i]);
}
//主函数
void main()
{
SqStack *sq; //先开始缺少这个,对sq的声明
int x,i;
InitStack(sq);
printf("请输入要进入栈的数字(输入-1结束)\n");
for(i=0;i<StackSize;i++)
{
printf("data=");
scanf("%d\n",&x); //通过键盘输入为变量x赋值
if(x==-1) break; //咋要输入2个-1呢,才出去呢???
else
Push(sq,x); //将x压入栈中
}
out_s(sq);
//printf("\n 进栈 x=?");
//scanf("%d",&x);
//Push(sq,x); out_s(sq);
}