队列入门以及代码的具体实现

1.概念

  • 队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(FirstIn First Out) 入队列:进行插入操作的一端称为队尾(Tail/Rear) 出队列:进行删除操作的一端称为队头(Head/Front);

在这里插入图片描述

2.实现

  • 用单链表实现队列
2.1入队
  • 思路:
  1. 首先判断之前有没有元素,如果没有元素,就直接作为头;
  2. 如果之前已经有元素,就相当于尾插法;
//添加(入队)
    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出队
  • 思路:
  1. 首先判断队列里面是否有元素;
  2. 如果有元素,因为队列先进先出,所以直接将头节点作为新的头就好;
 //出队
    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获取队头元素
  • 思路:获取队头元素就比较简单,只要有头,直接返回head节点的值;
//获取队头元素但是不删除
    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.完整源码

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());

    }
    }

猜你喜欢

转载自blog.csdn.net/qq_45665172/article/details/110207244