本篇內(nèi)容介紹了“怎么通過棧逆序字符串”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)專注于洛扎企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城開發(fā)。洛扎網(wǎng)站建設(shè)公司,為洛扎等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
package helloclean; public class StackX { private int maxSize; private char[] stackArray; private int top; public StackX(int max){ maxSize = max; stackArray = new char[maxSize]; top = -1; } public void push(char j) { stackArray[++top] = j; } public char pop() { return stackArray[top--]; } public char peek() { return stackArray[top]; } public boolean isEmpty() { return (top == -1); } }
package helloclean; public class Revever { private String input; private String output; public Revever(String in) { this.input = in; } public String rev() { int stackSize = input.length(); StackX stackX = new StackX(stackSize); for(int j = 0; j < input.length(); j ++) { char ch = input.charAt(j); stackX.push(ch); } output = ""; while (!stackX.isEmpty()) { char ch = stackX.pop(); output = output + ch; } return output; } }
package helloclean; import java.util.Scanner; public class ReverseApp { public static void main(String[] args) { String output = ""; Scanner scanner = new Scanner(System.in); while (true) { System.out.println("input:"); String input = scanner.nextLine(); if(input.equals("q")) { System.out.println("退出!"); break; } Revever revever = new Revever(input); output = revever.rev(); System.out.println("逆序: " + output); } } }
“怎么通過棧逆序字符串”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!