Stack
Stacks are special linear tables, which are linear tables that are only allowed to be inserted and deleted at one end .
The top of the stack that is allowed to be inserted and deleted is the bottom of the stack.
The insert of the stack is called the stack, and the deletion is called the stack.
The attribute is: LIFO, so the stack is also called the LIFO table, or the LIFO table (last in first out).
Because the stack is a linear table, There are also sequential tables and linked lists in two forms , generally we use sequential tables.
As you can see from the code, there is actually a change in the INSERT and delete operations compared to the sequential table.
#include <iostream>using namespacestd; Const intStack_size =5; classSqstack {Private: int*Elem; inttop; intstackSize; Public: voidInitsqstack ();//initialization of the sequential stack intGetTop ();//get top of stack element intLength ();//get the capacity of the current stack voidDestroysqstack ();//Destroy sequence Stack voidClearsqstack ();//emptying the sequential stack voidPush (intV);//Press Stack/stack/ into stack intPop ();//out of the stack voidInputsqstack (intn);//INPUT Element voidOutputsqstack ();//Traverse Output}; voidSqstack::initsqstack () {Elem=New int[Stack_size]; Top=-1; StackSize=Stack_size;} intSqstack::gettop () {if(top==-1) cout<<"Empty Stack"<<Endl; intE =Elem[top]; returne;}intsqstack::length () {if(top==-1) cout<<"Empty Stack"<<Endl; returntop+1;} voidsqstack::D estroysqstack () {Delete[] elem; Top=-1; StackSize=0; } voidSqstack::clearsqstack () {Top=-1;}voidSqstack::P Ush (intv) {if(top==stacksize-1) { int*Newstack; Newstack=New int[StackSize +1]; for(intI=0; i<=top;i++) {Newstack[i]=Elem[i]; } Delete[] elem; Elem=Newstack; StackSize++; } elem[++top]=v; } intsqstack::P op () {intv; if(top==-1) cout<<"Empty Stack"<<Endl; V=elem[top--]; returnv; } voidSqstack::inputsqstack (intN) { intA; cout<<"Enter numbers:"<<Endl; for(inti =0; I < n; i++) {cin>>A; Push (a); } }voidSqstack::outputsqstack () {if((Elem = = NULL) | | (top==-1)) cout<<"error occurred"<<Endl; Else { for(inti =0; I <= top; i++) {cout<<elem[i]<<" "; } cout<<Endl; } }voidMain () {Sqstack S; S.initsqstack (); S.inputsqstack (6); S.outputsqstack (); cout<<s.gettop () <<Endl; S.pop (); S.outputsqstack ();}
Data structure and Algorithm learning summary 04 LINEAR table---Stack