알파벳 역순 출력(창고의 응용)

1200 단어 창고
abcd 입력, dcba 출력
java 코드:
package ch03;

public class Reverse {

	private String input;

	public Reverse(String input) {
		this.input = input;
	}

	public String doReverse() {
		CharStack cs = new CharStack(input.length());
		for (int i = 0; i < input.length(); i++) {
			char ch = input.charAt(i);
			cs.push(ch);
		}

		String output = " ";
		while (!cs.isEmpty()) {
			char ch = (char) cs.pop();
			output += ch;
		}
		return output;

	}

	class CharStack {
		private int maxSize;

		private char[] arr;

		private int top;

		//     

		public CharStack(int size) {
			maxSize = size;
			arr = new char[maxSize];
			top = -1;
		}

		//     
		public void push(char value) {
			top++;
			arr[top] = value;

			// arr[++top]=value;

		}

		//     
		public long pop() {
			return arr[top--];//   arr[top],  top--

		}

		//       
		public long peek() {
			return arr[top];
		}

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

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

	public static void main(String[] args) {

		Reverse re = new Reverse("     !");
		System.out.println(re.doReverse());

	}

}

좋은 웹페이지 즐겨찾기