欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

循环队列的实现

程序员文章站 2022-07-14 14:01:22
...

循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。

/**
 * 循环队列
 */
public class MyCircularQueue {
    public int[] data;
    public int capacity;
    public int p_tail;
    public int p_head;
    public boolean firstAdd =true;
    /** Initialize your data structure here. Set the size of the queue to be k. */
    public MyCircularQueue(int k) {
        data = new int[k];
        capacity = 0;
        p_tail = -1;
        p_head = -1;

    }

    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        if(firstAdd){
            p_head = 0;
            p_tail = 0;
            firstAdd = false;
        }else{
            if(p_tail == data.length-1){
                p_tail = 0;
            }else{
                p_tail++;
            }
        }

        data[p_tail]=value;
        capacity++;
        return true;

    }

    /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        if(p_head == data.length-1){
            p_head = 0;
        }else{
            p_head++;
        }
        capacity--;
        return true;
    }

    /** Get the front item from the queue. */
    public int Front() {

        if(isEmpty()){
            return -1;
        }

        return data[p_head];
    }

    /** Get the last item from the queue. */
    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        return data[p_tail];
    }

    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        return capacity == 0;
    }

    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {

        return capacity == data.length;

    }

    public static void main(String[] args) {
        MyCircularQueue circularQueue = new MyCircularQueue(2); // 设置长度为 2

        System.out.println(circularQueue.enQueue(4));
        System.out.println(circularQueue.Rear());
        System.out.println(circularQueue.enQueue(9));
        System.out.println(circularQueue.deQueue());
        System.out.println(circularQueue.Front());


//        System.out.println(circularQueue.Front());
//        System.out.println(circularQueue.deQueue());
//        System.out.println( circularQueue.Front());
//        System.out.println(circularQueue.deQueue());
//        System.out.println(circularQueue.Front());
//        System.out.println(circularQueue.enQueue(4));
//        System.out.println(circularQueue.enQueue(2));
//        System.out.println(circularQueue.enQueue(2));
//        System.out.println(circularQueue.enQueue(3));
    }
}

 

 

相关标签: 队列