Programming problem: the use of a linked list to achieve stack

class the Node <E> { 
    E Data; 
    the Node <E> Next = null ;
     public the Node (Data E) {
         the this .data = Data; 
    } 
} 

class ListStack <E> { 
    the Node <E> Top = null ;
     public  Boolean empty ( ) {
         return Top == null ; 
    } 

    // head interpolation new node is inserted, to achieve stack 
    public  void Push (E Data) { 
        the node <E> = the newNode new new the node <E> (Data); 
        newNode.next= top;
        top = newNode;
    }

    public E pop(){
        if(this.empty()){
            return null;
        }
        E data = top.data;
        top = top.next;
        return data;
    }

    public E peek(){
        if(empty())
            return null;
        return top.data;
    }
}

 

Guess you like

Origin www.cnblogs.com/hetaoyuan/p/11668382.html