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

链表栈

发表于: 2015-06-29   作者:luyulong   来源:转载   浏览:
摘要: public class Node { private Object object; private Node next; public Node() { this.next = null; this.object = null; } public Object getObject() { return object; } public
public class Node {
	private Object object;
	private Node next;

	public Node() {
		this.next = null;
		this.object = null;
	}

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	public Node getNext() {
		return next;
	}

	public void setNext(Node next) {
		this.next = next;
	}
}

public class LinkStack {
	private Node top;

	public LinkStack() {
		top = new Node();
	}

	/**
	 * 清空操作
	 * */
	public void clear() {
		top = new Node();
	}

	/**
	 * 压栈操作
	 * */
	public void push(Object object) {
		Node node = new Node();
		node.setObject(object);
		node.setNext(top.getNext());
		top.setNext(node);
	}

	/**
	 * 出栈操作
	 * */
	public Node pop() {
		if (top.getNext() == null) {
			System.out.println("Stack is Empty!");
			return null;
		}
		Node returnNode = top.getNext();
		top.setNext(top.getNext().getNext());
		return returnNode;
	}

	/**
	 * 判断为空
	 * */
	public boolean isEmpty() {
		return (top.getNext() == null) ? true : false;
	}

}

public class Test {
	public static void main(String[] args) {
		LinkStack linkStack = new LinkStack();
		linkStack.push("1234");
		linkStack.push("12345");
		linkStack.push("123456");
		linkStack.push("1234567");
		linkStack.isEmpty();
	    System.out.println(linkStack.toString());
	    System.out.println("end");

	}
}

链表栈

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

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