`
shoppingbill
  • 浏览: 58303 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Stack 栈

阅读更多
栈(First-in,Last-out)
StackX.java
package test.stack;

public class StackX {

	private int maxSize;
	private int top;
	private String[] stackArr ;
	
	public StackX(int size) {
		maxSize = size;
		stackArr = new String[maxSize];
		top = -1;
	}
	
	public void push(String j){
		stackArr[++top] = j;
	}
	
	public String pop(){
		return stackArr[top--];
	}
	
	public String seek(){
		return stackArr[top];
	}
	
	public boolean isEmpty(){
		return (top == -1);
	}
	
	public boolean isFull(){
		return (top ==(maxSize-1));
	}

}



StackApp.java
package test.stack;

public class StackApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

//		StackX stack = new StackX(3);
//		stack.push("a");
//		stack.push("b");
//		stack.push("c");
//		while(!stack.isEmpty()){
//			System.out.println(stack.pop());	
//		}
//		
		String []arr = {"a","b","c"};
		StackX stack = new StackX(arr.length);
		for(int i=0; i<arr.length; i++){
			stack.push(arr[i]);
		}
		while(!stack.isEmpty()){
			System.out.println(stack.pop());
		}

		String hello ="Hello World";
		
		String[] res = hello.split(" ");
//		for(int i=0; i<res.length; i++){
//			System.out.println(res[i]);
//		}
		StackX stack2 = new StackX(res.length);
		
		for(int i=0;i<res.length;i++){
			stack2.push(res[i]);
		}
		while(!stack2.isEmpty()){
			System.out.println(stack2.pop());
			
		}
		
	}

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics