数据结构中的队列操作

2024年8月28日 | 14 分钟阅读

什么是队列?

队列是元素的逻辑分组,其中更新或更改在一侧(“尾部”)引入,而现有元素在相反的末端(“头部”)删除。当一个元素被引入队列时,它会从尾部移动到头部,准备好被下一个删除。队列中新获得的元素将不得不等到集合的末尾。这种排序方式也称为先进先出 (FIFO)(“先到先得”)。“先来后到”是它的另一种说法。

操作系统使用各种队列来管理系统内的事务。下一个任务通常使用排队方法进行计划,以便尽快运行程序,同时服务尽可能多的人。此外,当我们键入时,击键次数有时会超过屏幕上的文本。原因是计算机现在正忙于其他任务。击键被排入队列,最终按正确的顺序显示在屏幕上。

队列数据结构中的不同操作

队列数据结构支持的各种操作,这些操作可以帮助用户修改和操作队列中存在的数据,包括:

  • 入队操作:“入队”是指将新元素添加到队列中的行为。在售票处,新来的个人会在哪里排队等待?这个人会走到队伍后面站着。队列中的新元素类似地添加到队列的末尾。
  • 出队操作:出队是从队列中删除元素的过程。由于队列遵循 FIFO 原则,我们必须删除最先放入的队列成员。我们将删除头部元素,并将它后面的元素设为新的头部元素,因为最先添加的元素自然会在队列的头部。
  • 头部操作:此操作类似于堆栈中的 peek 操作,它返回第一个元素的值而不删除它。
  • isEmpty 操作:isEmpty() 函数用于检查队列是否为空。
  • isFull 操作:isFull() 函数用于检查队列是否已满。

在看到队列数据结构的所有不同有效操作后,让我们编写代码,以函数的形式在所需的编程语言中实现所有这些操作。

让我们开始用 Java 编程语言编写一个基本程序,该程序将具有模拟上述所有队列数据结构操作的函数。

Java 代码

输出

SIZE_OF_QUEUE of Queue_Data_Structure is 8


Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
1
Integer value for the Queue
67
Inserted 67

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
1
Integer value for the Queue
54
Inserted 54

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
1
Integer value for the Queue
45
Inserted 45

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
1
Integer value for the Queue
33
Inserted 33

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
1
Integer value for the Queue
21
Inserted 21

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
1
Integer value for the Queue
18
Inserted 18

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
1 
Integer value for the Queue
78
Inserted 78

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
1 
Integer value for the Queue
98
Inserted 98

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
2
Queue_Data_Structure::

front_index index-> 0
array_of_Queue -> 
67  54  45  33  21  18  78  98  
rear_index index-> 7

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
1
Integer value for the Queue
100
Queue_Data_Structure is full

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
3
67 Deleted

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert data in the Queue_Data_Structure Data Structure.
2. To display the data present in the Queue_Data_Structure Data Structure.
3. To perform the deQueue_Data_Structure operation on the Queue_Data_Structure Data Structure.
2
Queue_Data_Structure::

front_index index-> 0
array_of_Queue -> 
54  45  33  21  18  78  98  
rear_index index-> 6

Do you want to continue (Type y or n) 

n

现在让我们看看 C++ 代码来实现队列数据结构。

C++ 代码

输出

SIZE_OF_QUEUE of Queue_Data_Struct is 7.

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
1
Integer value for the Queue
56

Inserted 56

Do you want to continue (Type y or n) 
y

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
1
Integer value for the Queue
44

Inserted 44

Do you want to continue (Type y or n) 
y

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
1
Integer value for the Queue
32

Inserted 32

Do you want to continue (Type y or n) 
y

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
1
Integer value for the Queue
98

Inserted 98

Do you want to continue (Type y or n) 
y

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
1
Integer value for the Queue
342

Inserted 342

Do you want to continue (Type y or n) 
y

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
1
Integer value for the Queue
87

Inserted 87

Do you want to continue (Type y or n) 
y

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
1
Integer value for the Queue
12

Inserted 12

Do you want to continue (Type y or n) 
y

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
1
Integer value for the Queue
4
Queue_Data_Struct is full
Do you want to continue (Type y or n) 
y

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
2
Contents of the Queue_Data_Struct are::

front_index index-> 0
array_of_Queue -> 56  44  32  98  342  87  12  
rear_index index-> 6

Do you want to continue (Type y or n) 
y

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
3
deQueue_Data_Struct operation successful.

Deleted -> 56

Do you want to continue (Type y or n) 
y

Select any one of the operations::
1. To insert data in the Queue_Data_Struct Data Structure.
2. To display the data present in the Queue_Data_Struct Data Structure.
3. To perform the deQueue_Data_Struct operation on the Queue_Data_Struct Data Structure.
2
Contents of the Queue_Data_Struct are::

front_index index-> 1
array_of_Queue -> 44  32  98  342  87  12  
rear_index index-> 6

Do you want to continue (Type y or n) 
n

现在让我们看看 C 代码来实现队列数据结构。

C 代码

输出

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
1

Integer value for the Queue
23

Inserted -> 23
Do you want to continue (Type y or n)
y

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
1

Integer value for the Queue
22

Inserted -> 22
Do you want to continue (Type y or n)
y

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
1

Integer value for the Queue
87

Inserted -> 87
Do you want to continue (Type y or n)
y

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
1

Integer value for the Queue
12

Inserted -> 12
Do you want to continue (Type y or n)
y

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
1

Integer value for the Queue
90

Inserted -> 90
Do you want to continue (Type y or n)
y

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
1

Integer value for the Queue
78

Inserted -> 78
Do you want to continue (Type y or n)
y

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
1

Integer value for the Queue
56

Inserted -> 56
Do you want to continue (Type y or n)
y

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
1

Integer value for the Queue
45

Queue is Full!!
Do you want to continue (Type y or n)
y

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
2

Contents of the Queue are::

Queue elements are:
23  22  87  12  90  78  56  

Do you want to continue (Type y or n)
y

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
3

Dequeue Done.

Deleted : 23
Do you want to continue (Type y or n)
y

Select any one of the operations::
1. To insert data in the Queue Data Structure.
2. To display the data present in the Queue Data Structure.
3. To perform the deQueue operation on the Queue Data Structure.
2

Contents of the Queue are::

Queue elements are:
22  87  12  90  78  56  

Do you want to continue (Type y or n)
n

因此,在本文中,我们了解了队列数据结构以及我们可以对队列数据结构执行的各种操作,以修改和操作队列中存在的数据。在文章的后半部分,我们还用三种不同的编程语言(C、C++ 和 Java)编写了代码,其中队列数据结构的每个操作都实现为一个单独的函数,在我们需要对队列中的数据执行特定操作时调用该函数。