您的位置:首页 > 编程语言 > Java开发

java栈算法实现单词逆序输出

2016-03-18 00:00 453 查看
摘要: 栈,单词逆序输出,java

package com.lee.stack;

public class stackInitial<T>{

public char[] arr;
public int maxsize;
public int top;

public stackInitial(int max) {
arr = new char[max];
top = -1;
}

public void push(char number) {
arr[++top] = number;
}

public char pop() {
return arr[top--];
}

public boolean isEmpty() {
return (top == -1);
}

public boolean isFull() {
return (top == maxsize - 1);
}

public static void main(String[] args) {
stackInitial a_stack = new stackInitial(10);
a_stack.push('1');
a_stack.push('0');
a_stack.push('3');
a_stack.push('3');
a_stack.push('a');
a_stack.push('n');
while(!a_stack.isEmpty())
{
System.out.println(a_stack.pop());
}
}
}

package com.lee.stack;

public class reverseWord {

public String input;
public String reverse="";

public reverseWord(String input)
{
this.input = input;
}
public String Reverse()
{
stackInitial a = new stackInitial(input.length());
for(int i=0;i<input.length();i++)
{
a.push(input.charAt(i));
}
while(!a.isEmpty())
{
//System.out.println(a.pop());
reverse+=a.pop();
}
return reverse;
}
public int count()
{
return (input.length());
}

public static void main(String[] args) {
reverseWord a = new reverseWord("But you are a girl");

System.out.println(a.Reverse());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息