Queue entry and specific implementation of the code

team

1. Concept

  • Queue : A special linear table that only allows inserting data at one end and deleting data at the other end. The queue has FIFO (First In First Out) into the queue: the end of the insert operation is called the tail/rear ) Out of the queue: The end of the delete operation is called the head of the queue (Head/Front);

Insert picture description here

2. Realize

  • Realize queue with singly linked list
2.1 Join the team
  • Ideas:
  1. First judge whether there is an element before, if there is no element, it is directly used as the head;
  2. If there are elements before, it is equivalent to tail interpolation;
//添加(入队)
    public void offer(int val) {
    
    
        Node node = new Node(val);
        if(this.head == null) {
    
    
            this.head = node;
            this.tail = node;
            return;
        }
        this.tail.next = node;
        this.tail = node;
    }
2.2 Departure
  • Ideas:
  1. First determine whether there are elements in the queue;
  2. If there are elements, because the queue is first in, first out, the head node is directly used as the new head;
 //出队
    public int poll() {
    
    
        if(this.head == null) {
    
    
            throw new RuntimeException("队列为空\n");
        }
        int oldData = this.head.val;
        if(this.head.next == null) {
    
    
            this.head = null;
            this.tail = null;
        }else {
    
    
            this.head = this.head.next;
        }
        return oldData;
    }

2.3 Get the head element
  • Idea: It is relatively simple to get the head element of the team, as long as there is a head, directly return the value of the head node;
//获取队头元素但是不删除
    public int peek() {
    
    
        if(this.head == null) {
    
    
            throw new RuntimeException("队列为空\n");
        }
        return this.head.val;
    }
    //判断是否为空
    public boolean isEmpty() {
    
    
        if(this.head == null) {
    
    
            return true;
        }
        return false;
    }

3. Complete source code

import java.util.LinkedList;
import java.util.Queue;

//顺序栈 +  链式队列
class Node {
    
    
    public int val;
    public Node next;

    public Node(int val) {
    
    
        this.val = val;
    }
}
//链式(单链表)队列
class MyQueue {
    
    
    public Node head;//头
    public Node tail;//尾
    //添加(入队)
    public void offer(int val) {
    
    
        Node node = new Node(val);
        if(this.head == null) {
    
    
            this.head = node;
            this.tail = node;
            return;
        }
        this.tail.next = node;
        this.tail = node;
    }
    //出队
    public int poll() {
    
    
        if(this.head == null) {
    
    
            throw new RuntimeException("队列为空\n");
        }
        int oldData = this.head.val;
        if(this.head.next == null) {
    
    
            this.head = null;
            this.tail = null;
        }else {
    
    
            this.head = this.head.next;
        }
        return oldData;
    }
    //获取队头元素但是不删除
    public int peek() {
    
    
        if(this.head == null) {
    
    
            throw new RuntimeException("队列为空\n");
        }
        return this.head.val;
    }
    //判断是否为空
    public boolean isEmpty() {
    
    
        if(this.head == null) {
    
    
            return true;
        }
        return false;
    }

}


public class TestDemo3 {
    
    

    public static void main(String[] args) {
    
    
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);//入队
        myQueue.offer(2);
        myQueue.offer(3);
        myQueue.offer(4);
        System.out.println(myQueue.peek());//获取队头元素
        System.out.println(myQueue.poll());//出栈
        System.out.println(myQueue.poll());
        System.out.println(myQueue.poll());
        System.out.println(myQueue.poll());

    }
    }

Guess you like

Origin blog.csdn.net/qq_45665172/article/details/110207244