队列-Java实现

简介: 队列

队列,先进先出结构


public void offer(E data){
   
        Node<E> node  = new Node<>(data);
        if (head.next == null) {
   
            rear = head;
        }
        while (rear.next != null){
   
           rear = rear.next;
        }
        rear.next = node;
        rear = node;
        rear.next = null;
    }


 public E poll(){
   
        if (isEmpty()){
   
            throw new RuntimeException("队列为空");
        }
        E e = head.next.data;
        head.next = head.next.next;
        return e;
    }


  public E peek(){
   
        if (isEmpty()) {
   
            throw new RuntimeException("队列为空");
        }
        return head.next.data;
    }


总览

package com.collection;

/**
 * @author WangYH
 * @version 2021.1.3
 * @date 2023/4/30 9:50
 */

public class LinkedQueue<E> {
   

    private Node<E> head = new Node<>(null);
    private Node<E> rear = new Node<>(null);


    public void offer(E data){
   
        Node<E> node  = new Node<>(data);
        if (head.next == null) {
   
            rear = head;
        }
        while (rear.next != null){
   
           rear = rear.next;
        }
        rear.next = node;
        rear = node;
        rear.next = null;
    }

    public E poll(){
   
        if (isEmpty()){
   
            throw new RuntimeException("队列为空");
        }
        E e = head.next.data;
        head.next = head.next.next;
        return e;
    }

    public E peek(){
   
        if (isEmpty()) {
   
            throw new RuntimeException("队列为空");
        }
        return head.next.data;
    }

    public boolean isEmpty() {
   
        return head.next == null;
    }

    private static class Node<E> {
   
        private E data;
        private Node<E> next;

        public Node(E data) {
   
            this.data = data;
        }
    }
}


测试

package com.collection;

/**
 * @author WangYH
 * @version 2021.1.3
 * @date 2023/4/29 21:42
 */

public class Main {
   
    public static void main(String[] args) {
   
       LinkedQueue<String> queue = new LinkedQueue<>();
       queue.offer("A");
       queue.offer("B");
       queue.offer("C");
       queue.offer("D");

        System.out.println(queue.peek());
        while (!queue.isEmpty()) {
   
           System.out.println(queue.poll());
       }

    }
}
目录
相关文章
|
2天前
|
前端开发 Java
java中的Queue队列的用法
java中的Queue队列的用法
21 1
|
2天前
|
存储 安全 算法
解读 Java 并发队列 BlockingQueue
解读 Java 并发队列 BlockingQueue
23 0
|
2天前
|
Java
队列(JAVA)
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的性质。
23 0
|
5月前
|
Java
225. 用队列实现栈 --力扣 --JAVA
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。 实现 MyStack 类: void push(int x) 将元素 x 压入栈顶。 int pop() 移除并返回栈顶元素。 int top() 返回栈顶元素。 boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
46 1
|
7月前
|
存储 消息中间件 缓存
Java数据结构第三讲-栈/队列
Java数据结构第三讲-栈/队列
|
2天前
|
存储 安全 Java
Java多线程实战-从零手搓一个简易线程池(一)定义任务等待队列
Java多线程实战-从零手搓一个简易线程池(一)定义任务等待队列
|
2天前
|
存储 Java C++
Java集合篇之深度解析Queue,单端队列、双端队列、优先级队列、阻塞队列
Java集合篇之深度解析Queue,单端队列、双端队列、优先级队列、阻塞队列
26 0
|
2天前
|
消息中间件 Java API
RabbitMQ入门指南(五):Java声明队列、交换机以及绑定
RabbitMQ是一个高效、可靠的开源消息队列系统,广泛用于软件开发、数据传输、微服务等领域。本文主要介绍了Java声明队列、交换机以及绑定队列和交换机等内容。
36 0
|
2天前
|
Java
【Java】栈和队列的模拟实现(包括循环队列)
【Java】栈和队列的模拟实现(包括循环队列)
5 0
|
7月前
|
存储 Java 调度
Java 最常见的面试题:队列和栈是什么?有什么区别?
Java 最常见的面试题:队列和栈是什么?有什么区别?