//#include <stdio.h> // c 库 #include <stdlib.h> //maclloc 库 #include <iostream> // c++ 库 // 有本句 ,下面cout 前面可以没有 std:: using namespace std; #define MAXSIZE 4 typedef char Elem; //顺序栈 算法思想是使用两个指针分别指向栈底和栈顶 // 初始和判空 top=base , 判满 top - base = maxsize //push 时 栈不满就 存入数据并top指针++ ,pop时相反 typedef struct { Elem data[MAXSIZE]; Elem* Top, * Base; } Stack; void InitStack(Stack& s) { s.Base = s.Top = s.data; } //判空 bool StackEmpty(Stack s) { if (s.Top == s.Base) return 1; else return 0; } //判满 bool StackFull(Stack s) { if (s.Top - s.Base == MAXSIZE) { // cout << " S is Full !!" << endl; return 1; } else return 0; } bool Push(Stack& s, char e) { if (StackFull(s)) { cout << " 数据 " << e << " is LOST !" << endl; return 0; } else { *s.Top = e; (s.Top)++; //top 指针上移 } } bool Pop(Stack& s, Elem& x) { if (StackEmpty(s)) { cout << " S is Empty !!" << endl; return 0; } else { x = *(s.Top - 1); (s.Top)--; //top 指针下移 return 1; } } //Destroy(Stack &S); int main() { Stack S; //初始化 InitStack(S); //Push 测试 Push(S, 'a'); Push(S, 'b'); Push(S, 'c'); Push(S, 'd'); Push(S, 'e'); //POP 测试 Elem t; Pop(S, t); cout << "pop " << t << endl; Pop(S, t); cout << "pop " << t << endl; ////查看Stack内数据 //cout << endl << " Stack 内数据是"; //for (int i = 0; i < MAXSIZE; i++) // cout << S.data[i] << " "; //cout << endl; }