First, the characteristics of the stack
1, Stack is an advanced post-out (FILO) data structure
2, stack additions and deletions can only be operated from the top of the stack
Two, the stack has the following three kinds of operation
Push-pushes the element into the stack (adding elements to the top of the stack): first modify the pointer, then add the element
Peek-Returns the top element of the stack.
Pop-pops up the elements in the stack (returns and deletes the top element of the stack): Add the element First, then modify the pointer
Third, the implementation of the Java array method of the custom stack
ImportJava.lang.reflect.Array; Public classGeneralarraystack<t> { Private Static Final intDefault_size = 12; Privatet[] Marray; Private intcount; PublicGeneralarraystack (class<t>type) { This(type, default_size);//constructor method for calling two parameters } PublicGeneralarraystack (class<t> type,intsize) { //cannot directly use Marray = new T[default_size];Marray = (t[]) array.newinstance (type, size);//to construct an array of type T using the Reflection BuilderCount = 0; } //add Val to the stack Public voidpush (T val) {Marray[count++] =Val; } //returns the top element value of the stack PublicT Peek () {returnMarray[count-1]; } //returns the top element value of the stack and removes the top element of the stack PublicT Pop () {t ret= Marray[count-1]; Count--; returnret; } //returns the size of the stack Public intsize () {returncount; } //returns whether the "stack" is empty Public BooleanIsEmpty () {returnSize () ==0; } //print "Stack" Public voidPrintarraystack () {if(IsEmpty ()) {System.out.printf ("Stack is empty\n"); } System.out.printf ("Stack size () =%d\n", size ()); intI=size ()-1; while(i>=0) {System.out.println (marray[i]); I--; } } Public Static voidMain (string[] args) {String tmp; Generalarraystack<String> Astack =NewGeneralarraystack<string> (String.class); //Push 10, 20, 30 into the stack in turnAstack.push ("10"); Astack.push ("20"); Astack.push ("30"); //assigns the top element of the stack to TMP and deletes the top element of the stackTMP =Astack.pop (); System.out.println ("Tmp=" +tmp); //assign only the top of the stack to TMP, and do not delete the element.TMP =Astack.peek (); System.out.println ("Tmp=" +tmp); Astack.push ("40"); Astack. Printarraystack (); //Print Stack }}
Iv. Stacks provided in Java
ImportJava.util.Stack; Public classStacktest { Public Static voidMain (string[] args) {intTmp=0; Stack<Integer> Astack =NewStack<integer>(); //Push 10, 20, 30 into the stack in turnAstack.push (10); Astack.push (20); Astack.push (30); //assigns the top element of the stack to TMP and deletes the top element of the stackTMP =Astack.pop (); //System.out.printf ("tmp=%d\n", TMP); //assign only the top of the stack to TMP, and do not delete the element.TMP = (int) Astack.peek (); //System.out.printf ("tmp=%d\n", TMP);Astack.push (40); while(!Astack.empty ()) {tmp= (int) Astack.pop (); System.out.printf ("Tmp=%d\n", TMP); } }}
The introduction of stack and its Java implementation