"Fun from entry to advanced data structures" linked list queue

This article comes from liuyubobobo of "Fun from entry to advanced data structures" video tutorials

Linked list structure as shown below, contains a list node and attribute the next property e

Java code implemented using a linked list node

// 链表节点
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();
    }
}

Queue list by adding the first team needs to head, tail the tail, following FIG.

The current head points Node0, Node0 the next point Node1, i.e. node0.next = node1

java code implementation

//使用链表实现一个队列
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, the team

2, the Ensemble is a new Node node, so that next Node4 points to new Node node, while the tail can also point to the new node Node

 

 

Published 51 original articles · won praise 14 · views 40000 +

Guess you like

Origin blog.csdn.net/u010606397/article/details/98204488