标签:def false 栈的实现 span 入栈 max space int div
1 #define Max 50 2 #define ElemType int 3 #define OK 1 4 using namespace std; 5 6 typedef struct Stack //顺序栈的定义 7 { 8 int data[Max]; 9 int top; 10 }Stack; 11 12 void StackInit(Stack& s)//顺序栈的初始化 13 { 14 s.top = -1; 15 } 16 17 bool StackEmpty(Stack& s)//顺序栈是否为空 18 { 19 return s.top ==- 1; 20 } 21 22 bool StackFull(Stack& s)//顺序栈是否为满 23 { 24 return s.top == Max-1; 25 } 26 27 bool StackPush(Stack& s, ElemType e)//顺序栈入栈 28 { 29 if (StackFull(s)) 30 { 31 return false; 32 } 33 else 34 { 35 s.data[++(s.top)] = e; 36 return true; 37 } 38 } 39 40 bool StackPop(Stack& s)///顺序栈出栈 41 { 42 if (StackEmpty(s)) 43 { 44 return false; 45 } 46 else 47 { 48 --(s.top); 49 return true; 50 } 51 } 52 53 ElemType StackTop(Stack& s)//顺序栈栈顶元素查询 54 { 55 return s.data[s.top]; 56 } 57 58 void StackShow(Stack& s)//顺序栈的遍历 59 { 60 for (int i = 0; i <= s.top; ++i) 61 { 62 cout << s.data[i] << " "; 63 } 64 } 65 int main() { 66 Stack s; 67 StackInit(s); 68 StackPush(s, 2); 69 StackPush(s, 3); 70 StackPush(s, 4); 71 StackPush(s, 5); 72 StackShow(s); 73 cout << StackTop(s) << endl; 74 StackPop(s); 75 StackShow(s); 76 cout << StackEmpty(s) << endl; 77 return 0; 78 79 }
标签:def false 栈的实现 span 入栈 max space int div
原文地址:https://www.cnblogs.com/dhhu007/p/13195560.html