编程题:利用链表实现栈

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

class ListStack<E>{
    Node<E> top = null;
    public boolean empty(){
        return top == null;
    }

    //头插法插入新节点,实现入栈
    public void push(E data){
        Node<E> newNode = new 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;
    }
}

 

posted @ 2019-10-13 21:12  何浩源  阅读(285)  评论(0编辑  收藏  举报
//一下两个链接最好自己保存下来,再上传到自己的博客园的“文件”选项中