Queue队列的学习

参考于LeeTCode中的Queue: https://leetcode.com/explore/featured/card/queue-stack/228/first-in-first-out-data-structure/1355/

As shown in the picture above, the queue is a typical FIFO data stucture. The insert operation is also called enqueue and the new element is always added at the end of the queue. The delete operation is called dequeue. You are only allowed to remove the first element.

在这里插入图片描述

To implement a queue, we may use a dynamic array and an index pointing to the head of the queue.

As mentioned, a queue should support two operations: enqueue and dequeue. Enqueue appends a new element to the queue while dequeue removes the first element. So we need an index to indicate the starting point.

Here is an implementation for your reference:

#include <iostream>

class MyQueue {
    private:
        // store elements
        vector<int> data;      //先进行定义 
        // a pointer to indicate the start position
        int p_start;            //定义头指针
    public:
        MyQueue() {p_start = 0;}		//初始化队列
        /** Insert an element into the queue. Return true if the operation is successful. */
        bool enQueue(int x) {			//入队的函数
            data.push_back(x);
            return true;
        }
        /** Delete an element from the queue. Return true if the operation is successful. */
        bool deQueue() {			//出队的函数
            if (isEmpty()) {
                return false;
            }
            p_start++;
            return true;
        };
        /** Get the front item from the queue. */
        int Front() {			//返回队首元素
            return data[p_start];
        };
        /** Checks whether the queue is empty or not. */
        bool isEmpty()  {			//检测队列是否为空
            return p_start >= data.size();
        }
};

int main() {
    MyQueue q;		//创建一个队列对象
    q.enQueue(5);		//将5入队
    q.enQueue(3);		//将3入队
    if (!q.isEmpty()) {			//检测队列是否为空,不为空就输出队首元素的  
        cout << q.Front() << endl;			//这里会输出5
    }
    q.deQueue();		//调用一次出队,由于遵循的是FIFO,所以,这里是将5调出了队列
    if (!q.isEmpty()) {		
        cout << q.Front() << endl;		//同理,这里是输出3
    }
    q.deQueue();		//再一次将 3 出队
   
    if (!q.isEmpty()) {	
        cout << q.Front() << endl;		//由于这里队列中已经没有了元素,所以,这里将输出:已经没有了数据
    }
    else{
    	 cout <<“已经没有了数据” << endl;	
	}
}

conclusion:The implementation above is straightforward but is inefficient in some cases. With the movement of the start pointer, more and more space is wasted
意思就是说队列虽然使用起来很直接,但是,他效率不高,浪费了很大的空间,所以,我们更多的是使用下面的这种循环双端队列

Previously, we have provided a straightforward but inefficient implementation of queue.

A more efficient way is to use a circular queue. Specifically, we may use a fixed-size array and two pointers to indicate the starting position and the ending position. And the goal is to reuse the wasted storage we mentioned previously.

Let’s take a look at an example to see how a circular queue works. You should pay attention to the strategy we use to enqueue or dequeue an element.

双端队列的实现可以参考文献:
https://blog.csdn.net/nunchakushuang/article/details/11952661

猜你喜欢

转载自blog.csdn.net/xiao_jj_jj/article/details/82984466
今日推荐