《玩转数据结构 从入门到进阶》 链表实现队列

本文来源于liuyubobobo的“玩转数据结构 从入门到进阶”视频教程

链表结构如下图,链表节点包含一个e属性和next属性

使用java代码实现链表节点

// 链表节点
private class Node{
    // 当前节点的值
    public E e;
    // 当前节点的next属性指向下一个节点
    public Node next;

    public Node(E e, Node next){
       this.e = e;
       this.next = next;
    }
    public Node(E e){
       this.e = e;
       this.next = null;
    }
    public Node(){
       this.e = null;
       this.next = null;
    }

    @Override
    public String toString(){
        return e.toString();
    }
}

通过链表实现队列,需要添加队首head、队尾tail,如下图

当前head指向Node0,Node0中的next指向Node1,即node0.next=node1

java代码实现

//使用链表实现一个队列
public class LinkListQueue<E>{
    // 链表节点
    private class Node{
        // 当前节点的值
        public E e;
        // 当前节点的next属性指向下一个节点
        public Node next;

        public Node(E e, Node next){
           this.e = e;
           this.next = next;
        }
        public Node(E e){
           this.e = e;
           this.next = null;
        }
        public Node(){
           this.e = null;
           this.next = null;
        }

        @Override
        public String toString(){
            return e.toString();
        }
    }

    private Node head, tail;  //队首、队尾
    private int size;  //队列大小

    // 初始化队列
    public LinkListQueue(){
        head = null;
        tail = null;
        size = 0;
    }

    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    // 入队,查看文章后面的图解
    public void enqueue(E e){
        // tail == null  队列中没有元素
        if (tail == null){
            tail = new Node(e);
            head = tail;
        }else {
            // 队列中已经有元素了,末尾元素的next指向一个新建的Node节点
            tail.next = new Node(e);
            // 队列类中维护的tail属性(队尾)也要指向新建的节点
            tail = tail.next;
        }
        size++;  // 队列大小加一
    }

    //出队,查看文章后面的图解
    public E dequeue(){
        if (isEmpty())
            throw new RuntimeException("队列空了亲");

        //获取队首节点
        Node cur = head;
        // 队列类中维护的head属性(队首)指向下一节点
        head = head.next;
        // 删除出队节点的引用,以便java虚拟机回收内存空间
        cur.next = null;

        // 队首指向null,表明队列中没有元素了
        if (head == null)
            tail = null;

        size--;   // 队列大小减一
        // 返回队首节点值
        return cur.e;
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append("Queue: front ");

        Node cur = head;
        while(cur != null) {
            res.append(cur + "->");
            cur = cur.next;
        }
        res.append("NULL tail");
        return res.toString();
    }

    public static void main(String[] args){

        LinkListQueue<Integer> queue = new LinkListQueue<>();
        for(int i = 0 ; i < 10 ; i ++){
            queue.enqueue(i);
            System.out.println(queue);

            if(i % 3 == 0){
                queue.dequeue();
                System.out.println(queue);
            }
        }
        for(int i = 0 ; i < 7 ; i ++){
            queue.dequeue();
            System.out.println(queue);
        }
    }

}

1、出队

2、入队则是新增一个Node节点,让Node4的next指向新Node节点,同时tail也指向新Node节点即可

发布了51 篇原创文章 · 获赞 14 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/u010606397/article/details/98204488