#include <stdio.h> // c 库 #include <stdlib.h> //maclloc 库 #include <iostream> // c++ 库 // 有本句 ,下面cout 前面可以没有 std:: using namespace std; typedef char ElemType; //元素数据类型 char #define MAXSIZE 10 typedef struct ShareStack { ElemType Data[MAXSIZE]; int top1, top2; }SS; void Init(SS& S) { S.top1 = -1; S.top2 = MAXSIZE; } bool Push(SS& S, bool left, ElemType e) { if (S.top1 + 1 == S.top2) { //cout << " full!"; return 0; } else if (left) { S.Data[++S.top1] = e; } else { S.Data[--S.top2] = e; } return 1; } bool Pop(SS& S, bool left, ElemType& e) { if (left) { if (S.top1 == -1) return 0; e = S.Data[S.top1--]; } else { if (S.top2 == MAXSIZE) return 0; e = S.Data[S.top2++]; } } void main() { SS S; Init(S); Push(S, 1, 'a'); Push(S, 1, 'b'); Push(S, 1, 'c'); Push(S, 1, 'd'); Push(S, 1, 'e'); Push(S, 0, 'A'); Push(S, 0, 'B'); Push(S, 0, 'C'); Push(S, 0, 'D'); Push(S, 0, 'E'); Push(S, 0, 'F'); //测试查看数组 for (int i = 0; i < MAXSIZE; i++) { cout << "\n" << i << " " << S.Data[i]; } ElemType e; Pop(S, 1, e); cout << endl; cout << endl << " Pop " << e; Pop(S, 1, e); cout << endl; cout << endl << " Pop " << e; Pop(S, 0, e); cout << endl; cout << endl << " Pop " << e; Pop(S, 0, e); cout << endl; cout << endl << " Pop " << e; cout << endl; }