A stack is an abstract data structure that "goes first and out". For example: when we wash dishes, wash a plate, put it on the top of a stack of plates, but after we have all washed, if we have a plate, we will start from the top plate, this example is like the data structure of the stack, the first put in the information, and finally to get.
Stacks not only have many examples in life, they are used more frequently in computers. For example, most processors use a stack-based architecture, and when a method is called, the return address and parameters are pressed into the stack, and when the method finishes returning, those data are out of the stack. The stack operation is embedded in the microprocessor. There are also older calculators like this, they are not input parentheses in the arithmetic expression, but the intermediate results are first deposited in the stack and so on.
Stack schematic diagram:
Stack code implementation (there is also the code for the delimiter matching example below):
Stack class implementation:
PackageCom.dxx.stack; Public classStackx {Private intmaxSize; Private int[] stackarray; Private inttop; //Initializes an array of stacks in the constructor and resets the stack index to-1, indicating that the stack is empty. PublicStackx (intmaxSize) { Super(); This. maxSize =maxSize; Stackarray=New int[MaxSize]; Top=-1; } //determine whether the stack is empty, before the stack operation must first determine whether the stack is empty, if it is empty, it can not be out of the stack. Public BooleanIsEmpty () {if(top = =-1) return true; return false; } //determine whether the stack is full, before the stack operation must first determine whether the stack is full, if it is full, you can not into the stack. Public BooleanIsfull () {if(top = = MaxSize-1) return true; return false; } //into the stack Public voidPushintnum) { if(!Isfull ()) {stackarray[++top] =num; }Else{System.out.println ("The stack is full and can no longer be in the stack operation. "); } } //out of the stack Public intpop () {if(!IsEmpty ()) { returnstackarray[top--]; }Else{System.out.println ("Stack is empty, cannot get the stack fixed element");return0; } } //take the top element of the stack Public intPeek () {if(!IsEmpty ()) { returnStackarray[top]; }Else{System.out.println ("Stack is empty, cannot get the stack fixed element");return0; } }}
Main program test:
PackageCom.dxx.stack; Public classStackmain { Public Static voidMain (string[] args) {Stackx Stackx=NewStackx (5); Stackx.peek (); Stackx.push (0); Stackx.push (1); Stackx.push (2); Stackx.push (3); Stackx.push (4); Stackx.push (5); System.out.println (Stackx.pop ()); System.out.println (Stackx.pop ()); System.out.println (Stackx.pop ()); }}
Output Result:
The stack is empty, cannot get the stack is full, can not be in the stack operation. 432
Here is the example code for delimiter matching:
Analysis:
The class of the delimiter stack:
PackageCom.dxx.stack; Public classBracketstackx {Private intmaxSize; Private Char[] stackarray; Private inttop; //Initializes an array of stacks in the constructor and resets the stack index to-1, indicating that the stack is empty. PublicBracketstackx (intmaxSize) { Super(); This. maxSize =maxSize; Stackarray=New Char[MaxSize]; Top=-1; } //determine whether the stack is empty, before the stack operation must first determine whether the stack is empty, if it is empty, it can not be out of the stack. Public BooleanIsEmpty () {if(top = =-1) return true; return false; } //determine whether the stack is full, before the stack operation must first determine whether the stack is full, if it is full, you can not into the stack. Public BooleanIsfull () {if(top = = MaxSize-1) return true; return false; } //into the stack Public voidPushCharch) { if(!Isfull ()) {stackarray[++top] =ch; }Else{System.out.println ("The stack is full and can no longer be in the stack operation. "); } } //out of the stack Public Charpop () {if(!IsEmpty ()) { returnstackarray[top--]; }Else{System.out.println ("Stack is empty, cannot get the stack fixed element");return0; } } //take the top element of the stack Public CharPeek () {if(!IsEmpty ()) { returnStackarray[top]; }Else{System.out.println ("Stack is empty, cannot get the stack fixed element");return0; } }}
String matching Check class:
PackageCom.dxx.stack;//defines a toolbar that checks if a delimiter matches Public classBracketcheck {PrivateString input; PublicBracketcheck (String input) {Super(); This. Input =input; } Public BooleanChek () {//instantiate the stack of delimiters firstBracketstackx Stackx =NewBracketstackx (Input.length ()); //The character of the input is compared in turn, if it is left delimiter, on the stack, is the right delimiter, the stack and the right delimiter match. for(inti=0; I<input.length (); i++){ CharCH =Input.charat (i); Switch(CH) { Case‘{‘: Case‘[‘: Case‘(‘: Stackx.push (CH); Break; Case‘)‘: Case‘]‘: Case‘}‘: CharCH2 =Stackx.pop (); if(ch== ') ' &&ch2== ' (' | | ch== ' &&ch2== ' [' | | ch== '} ' &&ch2== ' {'){ }Else { return false; } Break; } } if(!Stackx.isempty ()) { return false; } return true; }}
Main program test:
Package Com.dxx.stack; Public class Stackmain { publicstaticvoid main (string[] args) { new Bracketcheck ("123{{[4444 (3333) fff]ddddd}"); if (Bracketcheck.chek ()) System.out.println ("Match"); Else System.out.println ("mismatch");} }
The result is: mismatch.
You can also practice a list in reverse order.
Stack implementation Java