1 #include<iostream> 2 #include<ctime> 3 using namespace std; 4 5 #define MAXSIZE 10 6 7 struct stack 8 { 9 int number[MAXSIZE]; 10 int top=-1; 11 }; 12 13 bool Push(stack *s, int e) 14 { 15 if (s->top == MAXSIZE - 1 || !s) 16 return false; 17 s->top++; 18 s->number[s->top] = e; 19 return true; 20 } 21 22 int Pop(stack *s) 23 { 24 if (!s||s->top==-1) 25 return -1; 26 int t = s->number[s->top]; 27 s->top--; 28 return t; 29 } 30 31 void InitStact(stack *s) 32 { 33 srand(time(0)); 34 for (int i = 0; i < MAXSIZE; i++) 35 { 36 s->number[i] = rand() % 100 + 1; 37 } 38 s->top = MAXSIZE - 1; 39 } 40 41 void DestroyStact(stack *s)//此处有问题 42 { 43 delete s; 44 } 45 46 void ClearStact(stack *s) 47 { 48 for (int i = 0; i <= s->top; i++) 49 s->number[i] = 0; 50 } 51 52 int GetTop(stack *s) 53 { 54 if (s->top != -1) 55 return s->number[s->top]; 56 else return -1; 57 } 58 void main() 59 { 60 stack *head = new stack; 61 InitStact(head); 62 for (int i = 0; i < MAXSIZE; i++) 63 cout << Pop(head)<<endl; 64 }