队列的特点以及实现

一:队列的含义以及特点

        我们直到栈是先进后出,后进先出的操作受限的线性表数据结构,而队列就是先进先出,后进后出的操作受限的线性表数据结构。队列也有两个操作,入队,放入一个数据到队列尾部;出队,从队列头部取一个元素。

二:队列的实现

        队列也有两种实现方式,数组和链表。用数组实现的队列叫做顺序队列,用链表实现的队列叫做链式队列。1.以下用数组的方式实现一个队列:

public class ArrayQueue{
    private String[] items;                       //数组
    private int n = 0;                            //数组大小
    private int head = 0;                         //头指针下标
    private int tail = 0;                         //尾指针下标
    
    public ArrayQueue(int capacity){              //初始化
        this.items = new String[capacity];
        this.n = capacity;
    }

    public boolean enqueue(String item){           //入队
        if(tail == n) return false;                //如果尾指针等于数组大小,说明队列已满
        items[tail] = item;                        //元素放在队尾
        ++tail;
        return true;
    }

    public String dequeue(){                       //出队
        if(head == tail) return null;              //头指针和尾指针相等,说明队列为空
        String temp = items[head];                 //去除头指针的元素
        ++head;
        return temp;
    }
}

随着不停地进行入队、出队操作,head 和 tail 都会持续往后移动。当 tail 移动到最右边,即使数组中还有空闲空间,也无法继续往队列中添加数据了,为了解决这个问题,我们可以使用数据搬移,但是,每次进行出队操作都相当于删除数组下标为 0 的数据,要搬移整个队列中的数据,这样出队操作的时间复杂度就会从原来的 O(1) 变为 O(n)。实际上,我们在出队时可以不用搬移数据。如果没有空闲空间了,我们只需要在入队时,再集中触发一次数据的搬移操作。借助这个思想,出队函数 dequeue() 保持不变,我们稍加改造一下入队函数 enqueue() 的实现,就可以轻松解决刚才的问题了。

// 入队操作,将item放入队尾 
public boolean enqueue(String item) { 
     
    if (tail == n) {                             // tail == n表示队列末尾没有空间了
        if (head == 0) return false;             // tail ==n && head==0,表示整个队列都占满了 
        for (int i = head; i < tail; ++i) {      // 数据搬移 
            items[i-head] = items[i]; 
        } 
        tail -= head;                            // 搬移完之后重新更新head和tail 
        head = 0; 
    } 
    items[tail] = item;
    ++tail; 
    return true; 
}

2.基于链表的队列实现方法:

基于链表的实现,我们同样需要两个指针:head 指针和 tail 指针。它们分别指向链表的第一个结点和最后一个结点。

/**
 * 基于链表实现的队列
 *
 */
public class QueueBasedOnLinkedList {

  // 队列的队首和队尾
  private Node head = null;
  private Node tail = null;

  // 入队
  public void enqueue(String value) {
    if (tail == null) {
      Node newNode = new Node(value, null);
      head = newNode;
      tail = newNode;
    } else {
      tail.next = new Node(value, null);
      tail = tail.next;
    }
  }

  // 出队
  public String dequeue() {
    if (head == null) return null;

    String value = head.data;
    head = head.next;
    if (head == null) {
      tail = null;
    }
    return value;
  }

  public void printAll() {
    Node p = head;
    while (p != null) {
      System.out.print(p.data + " ");
      p = p.next;
    }
    System.out.println();
  }

  private static class Node {
    private String data;
    private Node next;

    public Node(String data, Node next) {
      this.data = data;
      this.next = next;
    }

    public String getData() {
      return data;
    }
  }

}

3.循环队列:

    我们使用数组实现队列会有数据搬移操作,我们使用循环队列来避免这个问题,写好循环队列的关键在于确定好队空和队满的判定条件。我们发现队列为空的判断条件仍然是 head == tail,队满的判断条件是(tail+1)%n=head。当队列满时,tail 指向的位置实际上是没有存储数据的。所以,循环队列会浪费一个数组的存储空间。

public class CircularQueue {                          // 数组:items,数组大小:n 
    private String[] items; 
    private int n = 0;                                // head表示队头下标,tail表示队尾下标 
    private int head = 0; 
    private int tail = 0;               

    public CircularQueue(int capacity) {              // 申请一个大小为capacity的数组 
        items = new String[capacity]; 
        n = capacity; 
    } 
    
    public boolean enqueue(String item) {             // 入队 
        if ((tail + 1) % n == head) return false;     // 队列满了 
        items[tail] = item; tail = (tail + 1) % n; 
        return true; 
    } 
     
    public String dequeue() {                          // 出队 
        if (head == tail) return null;                 // 如果head == tail 表示队列为空 
        String ret = items[head]; 
        head = (head + 1) % n; 
        return ret; 
    }
}

三:阻塞队列和并发队列

阻塞队列其实就是在队列基础上增加了阻塞操作。简单来说,就是在队列为空的时候,从队头取数据会被阻塞。因为此时还没有数据可取,直到队列中有了数据才能返回;如果队列已经满了,那么插入数据的操作就会被阻塞,直到队列中有空闲位置后再插入数据,然后再返回。

线程安全的队列我们叫作并发队列。最简单直接的实现方式是直接在 enqueue()、dequeue() 方法上加锁,但是锁粒度大并发度会比较低,同一时刻仅允许一个存或者取操作。实际上,基于数组的循环队列,利用 CAS 原子操作,可以实现非常高效的并发队列。这也是循环队列比链式队列应用更加广泛的原因

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值