这里是代码:
#include<iostream>
using namespace std;
#define MAXSIZE 100
typedef struct{
int id;
char name[10];
}SElemtype;
typedef struct{
SElemtype *base;
SElemtype *top;
int stacksize;
}Sqstack;
void InitStack(Sqstack &S);
void Push(Sqstack &S,SElemtype e);
void Pop(Sqstack &S,SElemtype &e);
SElemtype Gettop(Sqstack S);
int main(){
Sqstack s;
SElemtype newdate;
cout << "构造一个空栈" << endl;
InitStack(s);
int i,n;
cout << "请输入栈的长度:" << endl;
cin >> n;
for(i=1;i<=n;i++){
cout << "输入栈的第" << i << "个元素" << endl;
++s.top;
cin >> newdate.id >> newdate.name;
Push(s,newdate);
}
cout << "请输出栈顶元素:"<< endl;
Gettop(s);
cout << newdate.id << " " << newdate.name << endl;
cout << "请输出元素:" << endl;
for(int i=1;i <= s.top-s.base;i++){
cout << newdate.id << " " << newdate.name << endl;
}
return 0;
}
//顺序栈的初始化
void InitStack(Sqstack &S){
S.base=new SElemtype[MAXSIZE];//为顺序栈的动态分配数组空间
if(!S.base)//存储分配失败
exit(1);
S.top=S.base;//top初始为base,空栈
S.stacksize=MAXSIZE;//stacksize置为栈的最大容量MAXSIZE
}
//顺序栈的入栈
void Push(Sqstack &S,SElemtype e){
if(S.top-S.base==S.stacksize)//栈满
exit(1);
*S.top++=e;
}
//顺序栈的出栈
void Pop(Sqstack &S,SElemtype &e){
if(S.top==S.base)//栈空
exit(1);
e=*--S.top;//栈顶指针减1,将栈顶元素赋值给e
}
//顺序栈获取栈顶元素
SElemtype Gettop(Sqstack S){
if(S.top != S.base)
return *(S.top-1);
}
顺序栈的输出,请问输出这里有什么问题啊?为什么输出的结果都是一样的?