1 /*Stacks and queues: logical structure is a linear table with limited operational constraints2 * Stack: Features advanced after out, only allowed on the top of the stack operation3 * How stacks are implemented: sequential stacks and chain stacks4 * Common operations: Into the stack, out of the stack, get the top of the stack, empty, sentenced to full, the capacity of the stack5 * Application of Stack6 * * Reverse-order related operations7 * * . Delimiter matching8 * */9 //Sequential StacksTen Public classMystack { One Private Long[] arr; A Private intMaxSize;//capacity of the stack - Private intTop//stack Top pointer - the PublicMystack (ints) { -MaxSize =s; -arr =New Long[maxSize]; -top =-1; + } - + Public BooleanIsfull () { A returntop = = MaxSize-1; at } - - Public BooleanIsEmpty () { - returntop = =-1; - } - in //the stack must be judged full first. - Public voidPushLongkey) { to if(!Isfull ()) { +Arr[++top] =key; - } the } * $ //before the stack to determine whether it is emptyPanax Notoginseng Public Longpop () { - Longnum = 0; the if(!IsEmpty ()) { +num = arr[top--]; A } the returnnum; + } - $ //get top of stack element $ Public LongGetpeek () { - returnArr[top]; - } the - //displaying the elements of a stackWuyi Public voidDiaplaystack () { the for(inti = top;i >= 0; i--){ -System.out.print (Arr[i] + ""); Wu } - System.out.println (); About } $ - Public intsize () { - returnTop + 1; - } A + Public LongGETPEEKN (intN) { the returnArr[n]; - } $ the Public voidDisplaystack (String s) { the System.out.println (s); theSystem.out.println ("stack----bottom to top"); the for(inti = 0; I < size (); i++){ -System.out.print (Getpeekn (i) + ""); in } the System.out.println (); the } About the}
Sequential stack--java Implementation