栈的应用之字符串的反转

Reverser.java

package sort;


public class Reverser {
private String input;
public Reverser(String in) {
// TODO Auto-generated constructor stub
input=in;
}

public String doRev(){
int stackSize=input.length();
StackX stack=new StackX(stackSize);
for(int i=0;i<stackSize;i++){
stack.push(input.charAt(i));
}
String output="";
while(!stack.isEmpty()){
output+=stack.pop();
}
return output;
}

}


class StackX{
private char[] arr;
private int maxSize;
private int top;
StackX(int m){
maxSize=m;
arr=new char[maxSize];
top=-1;
}
public void push(char val){
arr[++top]=val;
}
public char pop(){
return arr[top--];
}
public char peek(){
return arr[top];
}
public boolean isEmpty(){
return top==-1;
}
public boolean isFull(){
return top==maxSize-1;
}

}


Test.java

public class Test {

public static void main(String[] args) {

Reverser reverser=new Reverser("hello world");

System.out.println(reverser.doRev());

}

}

你可能感兴趣的:(栈的应用之字符串的反转)