LinkedStack

public class LinkedStack {

    private static class Node{
        U item;
        Nodenext;
        Node(){
            item=null;
            next=null;
        }
        Node(U u,Nodenode){
            item=u;
            next=node;
        }
        boolean end(){
            return item==null&&next==null;
        }
    }
    private Nodetop=new Node();
    public void push(T item){
        top=new Node(item,top);
    }

    public  T pop(){
        T result=top.item;
        if(!top.end()){
            top=top.next;
        }
        return result;
    }
    public static void main(String[] args) {
        LinkedStack ls=new LinkedStack();

        for(String s:"this is code test!!".split(" ")){
            ls.push(s);
        }
        String s;
        while ((s=ls.pop())!=null){
            System.out.println(s);
        }

    }
}

  

转载于:https://www.cnblogs.com/lIllIll/p/10653044.html

你可能感兴趣的:(LinkedStack)