java堆栈实现

原创
2012/10/04 17:13
阅读数 181
package stack;

public class Stack {
	private int maxSize;
	private int[] stackArray;
	private int top;

	public Stack(int maxSize) {
		maxSize = maxSize;
		stackArray = new int[maxSize];
		top = -1;
	}

	public void push(int i) {
		stackArray[++top] = i;
	}

	public int pop() {
		return stackArray[top--];
	}

	public int peak() {
		return stackArray[top];
	}

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

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

	public static void main(String[] args) {
		Stack stack = new Stack(10);
		for (int i = 0; i < 10; i++) {
			stack.push(i);
		}
		while (!stack.isEmpty()) {
			System.out.println(stack.pop());
		}
	}
}

展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部