#include"pch.h"
#include<iostream>
#define MaxSize 200
using namespace std;
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stackSize;
}SqStack;
bool InitStack(SqStack &S) {
S.base = new SElemType[MaxSize];
if (!S.base) return false;
S.top = S.base;
S.stackSize = MaxSize;
return true;
}
bool Push(SqStack &S, SElemType e) {
if (S.top - S.base == MaxSize)
return false;
*S.top++ = e;
return true;
}
bool Pop(SqStack &S, SElemType &e) {
if (S.top != S.base) {
e = *--S.top;
return true;
}
else
return false;
}
SElemType GetTop(SqStack S) {
if (S.top != S.base) {
return *(--S.top);
}
else return NULL;
}
void GetAll(SqStack S) {
int time = 0;
while (S.top != S.base) {
cout << *(--S.top) << " ";
++time;
}
cout << "\n栈元素数量" << time << endl;
}
int main() {
SqStack S; SElemType e;
if (InitStack(S))cout << "Succeed" << endl;
else cout << "Failed" << endl;
cout << "input e:\t";
while (cin >> e) {
Push(S, e);
cout << "input e:\t";
}
cout << "栈顶元素为" << GetTop(S) << endl;
cout << "栈所有元素为" << endl;
GetAll(S);
if (Pop(S, e))
cout << "移除的元素是" << e << endl;
else cout << "栈空" << endl;
cout << "栈剩下所有元素为" << endl;
GetAll(S);
return 0;
}