队列 数据结构_什么是队列数据结构?

队列是一种FIFO(先进先出)数据结构,常用于管理按顺序处理的对象,如打印机任务和CPU调度。入队(Enqueue)操作在队列尾部添加元素,出队(Dequeue)操作从头部移除元素。队列可以使用数组、堆栈或链表实现,其操作复杂度为O(1)。
摘要由CSDN通过智能技术生成

队列 数据结构

Queue is also an abstract data type or a linear data structure, just like stack data structure, in which the first element is inserted from one end called the REAR(also called tail), and the removal of existing element takes place from the other end called as FRONT(also called head).

队列也是抽象数据类型或线性数据结构,就像堆栈数据结构一样 ,其中第一个元素从称为REAR的一端(也称为tail )插入,而现有元素的删除从另一端进行称为FRONT (也称为head )。

This makes queue as FIFO(First in First Out) data structure, which means that element inserted first will be removed first.

这使队列成为FIFO (先进先出)数据结构,这意味着首先插入的元素将被首先移除。

Which is exactly how queue system works in real world. If you go to a ticket counter to buy movie tickets, and are first in the queue, then you will be the first one to get the tickets. Right? Same is the case with Queue data structure. Data inserted first, will leave the queue first.

队列系统在现实世界中正是这样工作的。 如果您去售票处购买电影票,并且排在第一位,那么您将是第一个获得票的人。 对? 队列数据结构也是如此。 首先插入的数据,将首先离开队列。

The process to add an element into queue is called Enqueue and the process of removal of an element from queue is called Dequeue.

将元素添加到队列中的过程称为Enqueue ,从队列中删除元素的过程称为Dequeue

Introduction to Queue

队列的基本功能 (Basic features of Queue)

  1. Like stack, queue is also an ordered list of elements of similar data types.

    像堆栈一样,队列也是类似数据类型的元素的有序列表。

  2. Queue is a FIFO( First in First Out ) structure.

    队列是一种FIFO(先进先出)结构。

  3. Once a new element is inserted into the Queue, all the elements inserted before the new element in the queue must be removed, to remove the new element.

    将新元素插入队列后,必须删除队列中新元素之前插入的所有元素,以删除新元素。

  4. peek( ) function is oftenly used to return the value of first element without dequeuing it.

    peek( )函数通常用于返回第一个元素的值而不会使其出队。

队列的应用 (Applications of Queue)

Queue, as the name suggests is used whenever we need to manage any group of objects in an order in which the first one coming in, also gets out first while the others wait for their turn, like in the following scenarios:

顾名思义,当我们需要按顺序管理任何一组对象时,就使用队列,第一个对象进入时,其他对象等待轮到时也首先离开,例如在以下情况下:

  1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.

    在单个共享资源上服务请求,例如打印机,CPU任务调度等。

  2. In real life scenario, Call Center phone systems uses Queues to hold people calling them in an order, until a service representative is free.

    在现实生活中,呼叫中心电话系统使用队列来按顺序呼叫呼叫人员,直到服务代表免费为止。

  3. Handling of interrupts in real-time systems. The interrupts are handled in the same order as they arrive i.e First come first served.

    实时系统中的中断处理。 中断的处理顺序与到达时相同,即先到先得。

队列数据结构的实现 (Implementation of Queue Data Structure)

Queue can be implemented using an Array, Stack or Linked List. The easiest way of implementing a queue is by using an Array.

可以使用数组,堆栈或链接列表来实现队列。 实现队列的最简单方法是使用数组。

Initially the head(FRONT) and the tail(REAR) of the queue points at the first index of the array (starting the index of array from 0). As we add elements to the queue, the tail keeps on moving ahead, always pointing to the position where the next element will be inserted, while the head remains at the first index.

最初,队列的 (FRONT)和尾部 (REAR)指向数组的第一个索引(从0开始数组的索引)。 当我们将元素添加到队列中时, 尾部将继续向前移动,始终指向将插入下一个元素的位置,而头部则保持在第一个索引处。

implementation of queue

When we remove an element from Queue, we can follow two possible approaches (mentioned [A] and [B] in above diagram). In [A] approach, we remove the element at head position, and then one by one shift all the other elements in forward position.

当我们从Queue中删除一个元素时,我们可以遵循两种可能的方法(在上图中提到[A]和[B])。 在[A]方法中,我们删除了位于头部位置的元素,然后将所有其他元素一一移位。

In approach [B] we remove the element from head position and then move head to the next position.

在方法[B]中,我们从头部位置移除元素,然后将头部移至下一个位置。

In approach [A] there is an overhead of shifting the elements one position forward every time we remove the first element.

在方法[A]中,每当我们删除第一个元素时,都需要将元素向前移动一个位置开销

In approach [B] there is no such overhead, but whenever we move head one position ahead, after removal of first element, the size on Queue is reduced by one space each time.

在方法[B]中,没有这种开销,但是只要我们将头向前移动一个位置,则在删除第一个元素之后,每次Queue大小都会减少一个空间

排队操作算法 (Algorithm for ENQUEUE operation)

  1. Check if the queue is full or not.

    检查队列是否已满。

  2. If the queue is full, then print overflow error and exit the program.

    如果队列已满,则打印溢出错误并退出程序。

  3. If the queue is not full, then increment the tail and add the element.

    如果队列未满,则增加尾部并添加元素。

DEQUEUE操作的算法 (Algorithm for DEQUEUE operation)

  1. Check if the queue is empty or not.

    检查队列是否为空。

  2. If the queue is empty, then print underflow error and exit the program.

    如果队列为空,则打印下溢错误并退出程序。

  3. If the queue is not empty, then print the element at the head and increment the head.

    如果队列不为空,则将元素打印在头部并递增头部。

/* Below program is written in C++ language */

#include<iostream>

using namespace std;

#define SIZE 10

class Queue
{
    int a[SIZE];
    int rear;   //same as tail
    int front;  //same as head
  
    public:
    Queue()
    {
        rear = front = -1;
    }
    
    //declaring enqueue, dequeue and display functions
    void enqueue(int x);     
    int dequeue();
    void display();
};

// function enqueue - to add data to queue
void Queue :: enqueue(int x)
{
    if(front == -1) {
        front++;
    }
    if( rear == SIZE-1)
    {
        cout << "Queue is full";
    }
    else
    {
        a[++rear] = x;
    }
}

// function dequeue - to remove data from queue
int Queue :: dequeue()
{
    return a[++front];  // following approach [B], explained above
}

// function to display the queue elements
void Queue :: display()
{
    int i;
    for( i = front; i <= rear; i++)
    {
        cout << a[i] << endl;
    }
}

// the main function
int main()
{
    Queue q;
    q.enqueue(10);
    q.enqueue(100);
    q.enqueue(1000);
    q.enqueue(1001);
    q.enqueue(1002);
    q.dequeue();
    q.enqueue(1003);
    q.dequeue();
    q.dequeue();
    q.enqueue(1004);
    
    q.display();
    
    return 0;
}

To implement approach [A], you simply need to change the dequeue method, and include a for loop which will shift all the remaining elements by one position.

要实现方法[A],您只需更改出dequeue方法,并包括一个for循环,该循环会将所有剩余元素移动一个位置。

return a[0];    //returning first element
for (i = 0; i < tail-1; i++)    //shifting all other elements
{
    a[i] = a[i+1];
    tail--;
}

队列操作的复杂度分析 (Complexity Analysis of Queue Operations)

Just like Stack, in case of a Queue too, we know exactly, on which position new element will be added and from where an element will be removed, hence both these operations requires a single step.

就像Stack一样,对于Queue,我们也确切知道,将在哪个位置添加新元素以及从哪里删除元素,因此这两个操作都需要一个步骤。

  • Enqueue: O(1)

    入队: O(1)

  • Dequeue: O(1)

    出队: O(1)

  • Size: O(1)

    尺码: O(1)

翻译自: https://www.studytonight.com/data-structures/queue-data-structure

队列 数据结构

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值