C++実装シーケンススタック


#include 
#include 
#include 
#include 
#include 
#define maxSize 100
using namespace std;

/*
Created by HarvestWu on 2018/5/4.
*/

//     
typedef struct
{
	int data[maxSize];	//      
	int top;		//  “  ”
}SqStack;			//     

//      
void initStack(SqStack &st);
//     
int isEmpty(SqStack st);
//     
int push(SqStack &st,int x);
//     
int pop(SqStack &st, int &x);
int main()
{
	SqStack st;
	int x;
	initStack(st);

	if (isEmpty(st))
		printf("  
"); else printf("
"); if (push(st,5)) printf("
"); else printf(" ,
"); if (isEmpty(st)) printf("
"); else printf("
"); if (pop(st, x)) printf(" , :%d
",x); else printf(" ,
"); return 0; } void initStack(SqStack &st) { st.top = -1;// -1 ( 0 ) } int isEmpty(SqStack st) { if (st.top == -1) return 1; else return 0; } int push(SqStack &st, int x) { if (st.top == maxSize - 1) // , return 0; st.data[++st.top] = x; // “ ”, return 1; } int pop(SqStack &st, int &x) { if (st.top == -1) // , return 0; x = st.data[st.top--]; // , “ ” return 1; }