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

原创
2016/03/18 16:15
阅读数 621
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());
	}
	
}


展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部