Stack sequential storage implementation
# Include
# Include
Using namespace std; # define STACK_SIZE 100 struct Stack {int * top; int * base; int Size;} Stack; /********************** Function Statements ************** * ******/bool InitStack (struct Stack & S ); // build the Stack bool IsEmpty (struct Stack & S); // judge the empty Stack bool IsFull (struct Stack & S); // judge whether the Stack is full void Push (struct Stack & S, int e); // press the Stack and insert the data void Pop (struct Stack & S, int & e); // Delete the data, that is, the Pop-up data bool DestroyStack (struct Stack & S ); // destroy Stack bool ClearStack (struct Stack & S); // clear Stack void PrintStack (struct Stack & S); // print the elements in the Stack, that is, output/********************main ************* * **********/int main () {struct Stack S; int B, n; if (InitStack (S) = false) {cout <"Init Stack error! "<Endl; exit (0) ;}cout <" push a data into stack: "<endl; cin> n; // cout <"Stack operation" <endl; while (n --) {Push (S, n); PrintStack (S );} // cout <"Stack operation" <endl; while (n <4) {Pop (S, B); PrintStack (S); n ++ ;} // clear stack cout <"Clear stack operation" <endl; if (ClearStack (S) PrintStack (S ); // destroy stack cout <"Destroy stack operation" <endl; DestroyStack (S); cout <"S. base = "<S. base <";" <"S. top = "<S. top <";" <" S. size = "<S. size <endl; return 0 ;} /************************************ print the elements in the stack, top-down display * that is, output from the end of the stack to the beginning of the stack *************************** * ***/void PrintStack (struct Stack & S) {int n = S. top-S. base; int index; // if the stack is empty, if (! N) {cout <".................................. "<endl; cout <" Stack is Empty... "<
= B, it indicates Stack Overflow ************************************ * *****/bool IsFull (struct Stack & S) {int A = S. top-S. base; int B = S. size; if (A >= B) {cout <"Stack is Full" <endl; return true;} else return false ;} /*************************************** * ******************** Push * stack operation * the stack is a special linear table, it does not support random access, so it can only be stored in order. * For example, the bullet in the bullet clip (this example is different from the implementation in this example: the top pointer of the stack remains unchanged, and the bottom pointer of the stack is swimming. * ++ In this program, the bottom pointer of the stack remains unchanged, and the top pointer of the stack is swimming .) **************************************** * *******************/Void Push (struct Stack & S, int e) {if (! IsFull (S) {* (S. top) = e; S. top ++ ;}} /*************************************** * *************************** Pop * stack operation * is a special linear table, it does not support random access, so it can only be stored in order. * For example, a bullet is introduced in the bullet clip (this example is different from the implementation in this example: the top pointer of the stack remains unchanged, and the bottom pointer of the stack is swimming. * ++ In this program, the bottom pointer of the stack remains unchanged, and the top pointer of the stack is swimming .) **************************************** * *************************/Void Pop (struct Stack & S, int & e) {if (IsFull (S) {cout <"Pop error" <endl;} else {e = * (S. top-1); S. top --;}}
Click Open Link