Java Data Structures and algorithms (3)-stacks (stacks and transpose)

Source: Internet
Author: User

The basic features of the stack are LIFO, the simplest use is for transpose, and other such as bracket matching, in-Order expressions (a+b* (c-d/(e+f))--and abcdef+/-*+) and subsequent expressions (345+*612+/-----(4+5) -6/(1 +2)) interchange and other advanced usage.

Example code:

 Packagechap04. Reverse;Importjava.io.*;//For I/OclassStackx {Private intmaxSize; Private Char[] stackarray; Private inttop;  PublicStackx (intmax) {MaxSize=Max; Stackarray=New Char[MaxSize]; Top=-1; }     Public voidPushCharj) {stackarray[++top] =J; }     Public Charpop () {returnstackarray[top--]; }     Public CharPeek () {returnStackarray[top]; }     Public BooleanIsEmpty () {return(top = =-1); }         Public BooleanIsfull () {return(top = = MaxSize-1); }}  classReverser {PrivateString input; PrivateString output;  PublicReverser (String in) {input=In ; }    // Transpose     PublicString Dorev () {intStackSize =input.length (); Stackx Thestack=NewStackx (stackSize);  for(intj = 0; J < Input.length (); J + +) {            CharCH =Input.charat (j);          Thestack.push (CH); } Output= "";  while(!Thestack.isempty ()) {            CharCH =Thestack.pop (); Output= output +ch; }        returnoutput; }  }  classReverseapp { Public Static voidMain (string[] args)throwsIOException {String input, output;  while(true) {System.out.print ("Enter A string:");            System.out.flush (); Input=getString1 (); if(Input.equals ("")) {                   Break; } reverser Thereverser=Newreverser (input); Output=Thereverser.dorev (); System.out.println ("Reversed:" +output); }     }      Public StaticString getString1 ()throwsIOException {InputStreamReader ISR=NewInputStreamReader (system.in); BufferedReader BR=NewBufferedReader (ISR); String s=Br.readline (); returns; }} 

Java Data Structures and algorithms (3)-stacks (stacks and transpose)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.