1 Public classStack {2 Private intmaxsize=2;//stack capacity, initially 2, (for expression evaluation, operand stack)3 Private intTop=-1;//stack Top pointer4 Private int[] Data=New int[MaxSize];//Data5 //empty sentence6 Public BooleanIsEmpty () {7 if(Top==-1)8 return true;9 ElseTen return false; One } A //Full sentence - Public BooleanIsfull () { - if(top==maxsize-1) the return true; - Else - return false; - } + //Tumbler Stack - Public BooleanPush (intelem) { + if(Isfull ()) A return false; at Else{ -data[++top]=Elem; - return true; - } - } - //out of the stack in Public intPop () { - if(IsEmpty ()) { to return-65535; + } - Else{ the returndata[top--]; * } $ }Panax Notoginseng}
Summarize:
- Class member variable to assign initial value (habit)
- MAXSIZE using macro definitions in C + +, arrays in Java do not require a predetermined length, but cannot be changed once the setting is modified (using static variables)
- Linear stack, via2015 winter grind
Java implementation of Stacks