package sort;
public class Stacks {
private int maxSize;
private char[] stackArray;
private int top;
Stacks(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char c) {
stackArray[++top] = c;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return top == -1;
}
}
package sort;
import java.io.*;
public class StackStringReverseApp {
public static void main(String[] args) throws IOException {
String input, output;
while(true) {
System.out.println("Enter a String:");
System.out.flush();
input = getString();
if(input.equals("")) break;
Reverse rs = new Reverse(input);
output = rs.doRev();
System.out.println("Reverse:" + output);
}
System.out.println("End...");
}
public static String getString() throws IOException {
InputStreamReader inReader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inReader);
String s = br.readLine();
return s;
}
}
class Reverse {
private String input;
private String output;
public Reverse(String in) {
input = in;
}
public String doRev() {
int stackSize = input.length();
Stacks stack = new Stacks(stackSize);
for(int i = 0; i < stackSize; i++) {
stack.push(input.charAt(i));
}
output = "";
while (!stack.isEmpty()) {
output += stack.pop();
}
return output;
}
}
运行结果:
Enter a String:
qwertyuio
Reverse:oiuytrewq
Enter a String:
1234567890
Reverse:0987654321
Enter a String:
End...