深入淺析java中的棧結構?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
java 數據結構中棧結構應用的兩個實例
1、單詞逆序。
要求從控制臺讀入一串字符,按回車結束輸入,同時顯示其逆序字符串。
對于顛倒順序的操作,用棧來解決是很方便的。具體思想是把字符串中的每一個字符按順序存入棧中,然后再一個一個的從棧中取出。這時就是按照逆序取出的字符串。
// reverse.java // stack used to reverse a string // to run this program: C>java ReverseApp import java.io.*; // for I/O //////////////////////////////////////////////////////////////// class StackX//定義了棧的基本結構和操作 { private int maxSize;//棧最大值 private char[] stackArray;//棧內用數組存儲數據 private int top;//當前棧頂標號,從0開始 //-------------------------------------------------------------- public StackX(int max) // constructor { maxSize = max; stackArray = new char[maxSize]; top = -1; } //-------------------------------------------------------------- public void push(char j) // put item on top of stack { stackArray[++top] = j; } //-------------------------------------------------------------- public char pop() // take item from top of stack { return stackArray[top--]; } //-------------------------------------------------------------- public char peek() // peek at top of stack { return stackArray[top]; } //-------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return (top == -1); } //-------------------------------------------------------------- } // end class StackX //////////////////////////////////////////////////////////////// class Reverser//封裝了單詞逆序的操作 { private String input; // input string private String output; // output string //-------------------------------------------------------------- public Reverser(String in) // constructor { input = in; } //-------------------------------------------------------------- public String doRev() // reverse the string { int stackSize = input.length(); // get max stack size StackX theStack = new StackX(stackSize); // make stack for(int j=0; j<input.length(); j++) { char ch = input.charAt(j); // get a char from input theStack.push(ch); // push it } output = ""; while( !theStack.isEmpty() ) { char ch = theStack.pop(); // pop a char, output = output + ch; // append to output } return output; } // end doRev() //-------------------------------------------------------------- } // end class Reverser //////////////////////////////////////////////////////////////// class ReverseApp { public static void main(String[] args) throws IOException { String input, output; while(true) { System.out.print("Enter a string: "); System.out.flush(); input = getString(); // read a string from kbd if( input.equals("") ) // 若沒有輸入字符串直接按回車,則結束 break; // make a Reverser Reverser theReverser = new Reverser(input); output = theReverser.doRev(); // use it System.out.println("Reversed: " + output); } // end while System.out.println("this is end"); } // end main() //-------------------------------------------------------------- public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } //-------------------------------------------------------------- } // end class ReverseApp ////////////////////////////////////////////////////////////////
2.分隔符匹配
有些分割符在編程中一定是成對出現的,例如(),{},和[]等。如果發現有未匹配的分隔符,編譯器會報錯。因為匹配操作采取就近原則,后輸入的分割符優先匹配,具有“后進先出”的特點。這個匹配操作可以用棧來實現。
具體操作是在輸入過程中,如果遇到左匹配符,則將左匹配符壓入棧中。如果遇到右匹配符,則從棧中取出一個數據,分析其與右匹配符是否相匹配。若匹配,則繼續進行,若不匹配,則報錯終止。
// brackets.java // stacks used to check matching brackets // to run this program: C>java bracketsApp import java.io.*; // for I/O //////////////////////////////////////////////////////////////// class StackX { private int maxSize; private char[] stackArray; private int top; //-------------------------------------------------------------- public StackX(int s) // constructor { maxSize = s; stackArray = new char[maxSize]; top = -1; } //-------------------------------------------------------------- public void push(char j) // put item on top of stack { stackArray[++top] = j; } //-------------------------------------------------------------- public char pop() // take item from top of stack { return stackArray[top--]; } //-------------------------------------------------------------- public char peek() // peek at top of stack { return stackArray[top]; } //-------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return (top == -1); } //-------------------------------------------------------------- } // end class StackX //////////////////////////////////////////////////////////////// class BracketChecker { private String input; // input string //-------------------------------------------------------------- public BracketChecker(String in) // constructor { input = in; } //-------------------------------------------------------------- public void check() { int stackSize = input.length(); // get max stack size StackX theStack = new StackX(stackSize); // make stack for(int j=0; j<input.length(); j++) // get chars in turn { char ch = input.charAt(j); // get char switch(ch) { case '{': // opening symbols case '[': case '(': theStack.push(ch); // push them break; case '}': // closing symbols case ']': case ')': if( !theStack.isEmpty() ) // if stack not empty, { char chx = theStack.pop(); // pop and check if( (ch=='}' && chx!='{') || (ch==']' && chx!='[') || (ch==')' && chx!='(') )//分隔符不匹配 System.out.println("Error: "+ch+" at "+j); } else // prematurely empty System.out.println("Error: "+ch+" at "+j); break; default: // no action on other characters break; } // end switch } // end for // at this point, all characters have been processed if( !theStack.isEmpty() ) System.out.println("Error: missing right delimiter"); } // end check() //-------------------------------------------------------------- } // end class BracketChecker //////////////////////////////////////////////////////////////// class BracketsApp { public static void main(String[] args) throws IOException { String input; while(true) { System.out.print( "Enter string containing delimiters: "); System.out.flush(); input = getString(); // read a string from kbd if( input.equals("") ) // quit if [Enter] break; // make a BracketChecker BracketChecker theChecker = new BracketChecker(input); theChecker.check(); // check brackets } // end while } // end main() //-------------------------------------------------------------- public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } //-------------------------------------------------------------- } // end class BracketsApp
關于深入淺析java中的棧結構問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。