java数据结构——Stack栈(单链表实现)

package com.tig.stack;


public class LinkedStack<E> {
	
	transient Node<E> first;
	
	public LinkedStack() {
		first = new Node<>();
	}
	
	public boolean push(E data) {
		
		Node<E> node = new Node<E>(data, first.getNext());
		
		first.setNext(node);
		return true;
	}
	
	public E pop() {
		
		if (first.getNext() == null) {
			return null;
		}
		
		Node<E> node = first.getNext();
		first.setNext(first.getNext().getNext());
		return node.getData();
	}
	
    private static class Node<E> {
    	private E data;	
    	
    	private Node<E> next;        //下一个节点
    	
    	public Node(E data, Node<E> next) {
    		super();
    		this.data = data;
    		this.next = next;
    	}

    	public Node() {
    		
    	}

    	public E getData() {
    		return data;
    	}

    	public void setData(E data) {
    		this.data = data;
    	}

    	public Node<E> getNext() {
    		return next;
    	}

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

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