#include <iostream> //引入头文件
using namespace std;typedef int Elemtype;#define Maxsize 10
#define ERROR 0
#define OK 1typedef struct
{Elemtype data[Maxsize];int top;
}SqStack;void InitStack(SqStack& S)
{S.top = -1;
}
bool StackEmpty(SqStack S)
{if (S.top == -1) //堆空return OK;else //不空return ERROR;
}bool Push(SqStack& S, Elemtype x)
{if (S.top == Maxsize - 1)return ERROR;S.data[++S.top] = x;return OK;
}bool Pop(SqStack& S, Elemtype& x)
{if (S.top == -1)return ERROR;x = S.data[S.top--];return OK;
}bool GetTop(SqStack& S, Elemtype& x)
{if (S.top == -1)return ERROR;x = S.data[S.top];return OK;
}int main(void)
{SqStack S;InitStack(S);Push(S, 1);Push(S, 2);Push(S, 3);Push(S, 4);Push(S, 5);int top = 0;GetTop(S, top);cout << "stack top: " << top << endl;if (StackEmpty(S) == 1)cout << "Stack is empty"<< endl;elsecout << "Stack is not empty"<< endl;int x = 0;Pop(S, x);cout << "pop1: " << x << endl;Pop(S, x);cout << "pop2: " << x << endl;Pop(S, x);cout << "pop3: " << x << endl;Pop(S, x);cout << "pop4: " << x << endl;Pop(S, x);cout << "pop5: " << x << endl;if (StackEmpty(S) == 1)cout << "Stack is empty" << endl;elsecout << "Stack is not empty" << endl;return 0;
}//#include <iostream> //引入头文件
//using namespace std;
//
//typedef int Elemtype;
//
//#define Maxsize 10
//#define ERROR 0
//#define OK 1
//
//typedef struct
//{
// Elemtype data[Maxsize];
// int top;
//}SqStack;
//
//
//void InitStack(SqStack& S)
//{
// S.top = 0;
//}
//bool StackEmpty(SqStack S)
//{
// if (S.top == 0) //堆空
// return OK;
// else //不空
// return ERROR;
//}
//
//bool Push(SqStack& S, Elemtype x)
//{
// if (S.top == Maxsize)
// return ERROR;
// S.data[S.top++] = x;
// return OK;
//}
//
//bool Pop(SqStack& S, Elemtype& x)
//{
// if (S.top == 0)
// return ERROR;
// x = S.data[--S.top];
// return OK;
//}
//
//bool GetTop(SqStack& S, Elemtype& x)
//{
// if (S.top == 0)
// return ERROR;
// x = S.data[S.top-1];
// return OK;
//}
//
//int main(void)
//{
// SqStack S;
// InitStack(S);
// Push(S, 1);
// Push(S, 2);
// Push(S, 3);
// Push(S, 4);
// Push(S, 5);
// int top = 0;
// GetTop(S, top);
// cout << "stack top: " << top << endl;
// if (StackEmpty(S) == 1)
// cout << "Stack is empty" << endl;
// else
// cout << "Stack is not empty" << endl;
// int x = 0;
// Pop(S, x);
// cout << "pop1: " << x << endl;
// Pop(S, x);
// cout << "pop2: " << x << endl;
// Pop(S, x);
// cout << "pop3: " << x << endl;
// Pop(S, x);
// cout << "pop4: " << x << endl;
// Pop(S, x);
// cout << "pop5: " << x << endl;
// if (StackEmpty(S) == 1)
// cout << "Stack is empty" << endl;
// else
// cout << "Stack is not empty" << endl;
// return 0;
//}