队列【Java】

36 篇文章 0 订阅
12 篇文章 2 订阅

什么是队列

队列是一种数据结构,具有先进先出的特点;
队列只允许在队头处出队列,在队尾处进队列;

模拟实现一个队列

模拟实现队列可以使用链表或者顺序表来实现,使用链表又可以使用单链表和双向链表来实现,这里我们使用单链表来实现:

public class test {
    //设置结点
   class Node {
       private int val;
       private Node next;
       private  Node(int val){
           this.val=val;
       }
   }

   //设置链表的头结点和尾结点
   private Node head;
   private Node last;
   //链表的长度
   int UsedSize;

    //入队
   public boolean offer(int val){

       Node node=new Node(val);

       //当前链表为空时
       if(head==null){
           head=node;
           last=node;
           return true;
       }else{
           last.next=node;      //从队尾入,让链表的为结点指向新的结点
           last=last.next;      //更新尾结点
           UsedSize++;          //长度加一
           return true;
       }

   }

   //出队
    public int poll(){

       if(empty()){
           System.out.println("队列为空");
           return -1;
       }else{
           int val=head.val;        //从队头出,记录当前的头结点的值
           head=head.next;          //更新头结点
           UsedSize--;              //长度减一
           return val;
       }


    }

    public int peek(){

        if(empty()){
            System.out.println("队列为空");
            return -1;
        }else{
            int val=head.val;
            return val;
        }

    }

    public boolean empty(){
       return UsedSize==0;
    }

    public int getUsedSize(){
       return UsedSize;
    }
}

队列的使用

java为队列的使用提供了一些方法:

在这里插入图片描述
一般比较常用的是上面标红的三个:

  public static void main(String[] args) {


        Queue<Integer> queue=new LinkedList<>();

        //入队
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        queue.offer(5);
        //出队

        queue.poll();   // 1

        //查看队头元素
        queue.peek();   // 2
        
        //队列长度,队列元素个数
        queue.size();   // 4
        
        //队列是否为空
        queue.isEmpty();    //false
        
    }
  public static void main(String[] args) {
        
        Queue<Integer> queue=new LinkedList<>();

        //入队
        queue.add(5);
        queue.add(4);
        queue.add(3);
        queue.add(2);
        queue.add(1);

        //出队

        Integer t=queue.remove();
        System.out.println(t);        // 5
        //查看队头元素
        Integer m=queue.element();
        System.out.println(m);          // 4

    }

这样2组方法实际实现的效果大同小异,但还是有一些细微的差别:

  • add()与offer():当在一个有容量限制的队列插入元素时,如果队列已满,使用add()就会直接抛出异常;
  • remove()与poll():当队列为空时,使用第一种方法会抛出异常;而poll()方法是返回一个null;
  • element()与peek():当队列为空时,使用第一种方法会抛出异常;而poll()方法是返回一个null;

循环队列

什么是循环队列

队列的首尾相接的顺序存储结构称为循环队列;

类似就是这样一个结构,front代表队头,rear代表队尾;

在这里插入图片描述
由于循环队列首尾相接的特殊性,我们必须考虑到这种队列空和满的情况,不难发现,当front=rear时表示队列为空,但是当队列满时,似乎也是满足这样的条件,因此我们使用一些方法重新来判断队列是空还是满:

  • 借助队列的长度,当rear移动的步数与队列的长度相等时,自然就表示队列已满;
  • 设置一个标志位flag:当frontrear并且flagtrue时表示队列为空;当frontrear并且flagfalse时表示队列为满;
  • 在队列中设置一个空闲位置:当队列空时,front==rear;当队列只剩一个空闲时,队列满;

模拟实现循环队列

public class CircularQueue {
    int [] elem;
    private int front;  //队头元素的下标
    private int rear;   //队尾元素的下标

    //key 数组大小
    public CircularQueue(int key){
       this.elem=new int[key];
    }

    //入队
    public boolean enQueue(int val){
        //判断队列是否满
        if(isFull()){
            return false;
        }
        //将元素放到rear下标处
        this.elem[rear]=val;
        rear=(rear+1)%elem.length;
        return true;
    }

    //出队
    public boolean deQueue(){
        if (empty()){
            return false;
        }else{
            front=(front+1)%elem.length;
            return  true;
        }
    }

    //查看队头元素
    public int Front(){
        if (empty()){
            return -1;
        }else{

            return  elem[front];
        }
    }
    
    //是否队满
    public boolean isFull(){
        if((rear+1)%elem.length==front){
            return true;
        }
        return false;
    }


    //是否队空
    public boolean empty(){
        if (front==rear){
            return true;
        }
        return false;
    }

}

over !

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
延迟队列(DelayQueue)是 Java 并发包中提供的一种队列实现,用于存储具有延迟时间的元素。延迟队列中的元素只有在其指定的延迟时间过去后才能被取出。 下面是一个简单的延迟队列Java 实现示例: 首先,我们需要定义一个实现了 Delayed 接口的延迟元素类: ```java import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; public class DelayedElement implements Delayed { private String data; private long startTime; public DelayedElement(String data, long delay) { this.data = data; this.startTime = System.currentTimeMillis() + delay; } @Override public long getDelay(TimeUnit unit) { long diff = startTime - System.currentTimeMillis(); return unit.convert(diff, TimeUnit.MILLISECONDS); } @Override public int compareTo(Delayed other) { if (this.startTime < ((DelayedElement) other).startTime) { return -1; } if (this.startTime > ((DelayedElement) other).startTime) { return 1; } return 0; } // Getter and Setter methods } ``` 然后,我们可以使用 DelayQueue 来存储延迟元素,并在需要时取出元素: ```java import java.util.concurrent.DelayQueue; public class DelayQueueExample { public static void main(String[] args) throws InterruptedException { DelayQueue<DelayedElement> delayQueue = new DelayQueue<>(); // 添加延迟元素到队列 delayQueue.add(new DelayedElement("Element 1", 2000)); // 延迟 2 秒 delayQueue.add(new DelayedElement("Element 2", 5000)); // 延迟 5 秒 delayQueue.add(new DelayedElement("Element 3", 1000)); // 延迟 1 秒 // 取出延迟元素并处理 while (!delayQueue.isEmpty()) { DelayedElement element = delayQueue.take(); System.out.println("Processing element: " + element.getData()); } } } ``` 在上面的示例中,我们创建了一个 DelayQueue 对象,并向其中添加了三个延迟元素。然后,我们使用 `take()` 方法从队列中取出元素,并进行处理。如果队列为空,`take()` 方法会阻塞等待,直到有延迟元素可用。 请注意,延迟时间的单位可以根据需要进行调整,例如使用 `TimeUnit.SECONDS` 表示秒。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值