链表栈

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");

	}
}

你可能感兴趣的:(java,数据结构)