内容仅供个人复习
#include<iostream>
#define MAXSIZE 100
using namespace std;
typedef struct
{
int *base;
int *top;
int stackSize;
}SqStack;
void Initstack( SqStack &s) // 初始化栈
{
s.base = new int[MAXSIZE];
s.top = s.base;
s.stackSize = MAXSIZE;
}
void Push( SqStack &s , int u) // 插入元素
{
if(s.top - s.base == s.stackSize) {cout<<"栈满\n"; return; }
*s.top = u;
s.top++;
}
void Pop(SqStack &s , int &e) //弹出栈
{
if(s.top == s.base)
{
cout<<"栈空\n";
return;
}
e = *--s.top;
}
int GetTop(SqStack s)
{
if(s.top != s.base)
{
return *(s.top-1);
}
}
int main()
{
SqStack s;
Initstack(s);
int e = 0;
cout<<"输入插入元素,一直到输入-1:\n";
while(e != -1)
{
cin>>e;
if(e != -1)
Push(s,e);
}
cout<<"弹出栈:";
Pop(s,e);
cout<<e<<'\n';
cout<<"取出栈顶元素:";
cout<<GetTop(s);
return 0;
}