原理比较简单,不在叙述。
代码如下:
#include<iostream> #include<stdlib.h> using namespace std; const int MAX = 1000; struct my_stack { int data[MAX]; int top; }; my_stack Init_stack() { my_stack s; s.top = -1; return s; } void push(my_stack &s, int value) { ++s.top; s.data[s.top] = value; } bool stack_empty(my_stack s) { return s.top == -1; } int pop(my_stack &s) { try { throw stack_empty(s); } catch(bool) { cerr << "The stack is empty!\n" ; exit(1); } --s.top; return s.data[s.top+1]; } void main() { int a[] = {1,3,5,7,9,10}; int size = sizeof(a)/sizeof(*a); my_stack s = Init_stack(); cout << pop(s); }