1 // array implement stack 2 # include <iostream> 3 using namespace STD; 4 5 struct sqstack {6 int data [100]; 7 int * base; 8 int * top; 9 int Len; // the currently used length is 10}; 11 void initstack (sqstack & S) {12 s. base = S. data; 13 s. top = S. base; 14 S. len = 0; 15} 16 17 void push (sqstack & S, int e) // inbound stack 18 {19 if (S. len <100) 20 {21 * s. top = E; 22 s. top ++; 23 S. len ++; 24} 25 return 0; 26} 27 28 int POP (sqstack & S) // out of the stack, note the direct reference, 29 {30 if (S. base = S. top) 31 return-1; 32 int e = * (S. top-1); 33 S. top --; 34 S. len --; 35 return E; 36} 37 38 void main () {39 40 sqstack s; 41 initstack (s ); 42 cout <"Enter less than 100 numbers (CTRL + d end input):" <Endl; 43 int temp; 44 45 while (CIN> temp) // Ctrl + d end 46 {push (S, temp); 47} 48 49 // output stack 50 cout <POP (s) <Endl; // output stack 151 cout <POP (s) <Endl; // output stack 252 cout <POP (s) <Endl; // output stack 353}
Sequential stack (2) own