当前位置:首页 > 开发 > 编程语言 > Java > 正文

一个简单的java栈

发表于: 2015-06-28   作者:luyulong   来源:转载   浏览:
摘要: public class MyStack { private long[] arr; private int top; public MyStack() { arr = new long[10]; top = -1; } public MyStack(int maxsize) { arr = new long[maxsize]; top
public class MyStack {
	private long[] arr;

	private int top;

	public MyStack() {
		arr = new long[10];
		top = -1;
	}

	public MyStack(int maxsize) {
		arr = new long[maxsize];
		top = -1;
	}

	// add
	public void push(int value) {
		arr[++top] = value;
	}

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

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

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

	public boolean isFull() {
		return top == arr.length - 1;
	}

	public static void main(String[] args) {
		MyStack myStack = new MyStack(4);
		myStack.push(1);
		myStack.push(2);
		myStack.push(3);
		myStack.push(4);
		System.out.println(myStack.isEmpty());
		System.out.println(myStack.isFull());
		System.out.println(myStack.peek());
		System.out.println(myStack.peek());
		while (!myStack.isEmpty()) {
			myStack.pop();
		}
		System.out.println(myStack.isEmpty());
		System.out.println(myStack.isFull());
	}
}

一个简单的java栈

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

推荐文章
编辑推荐
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号