Javatpoint Logo
Javatpoint Logo

Queue Operations in Data Structure

What is a Queue?

A queue is a logical group of elements in which updates or changes are introduced at one side (the "back") and existing items are deleted at the opposite end (the "front") (the "front"). When an item is introduced to the queue, it moves from the back to the front, ready to be eliminated next. The queue's newly acquired item will have to wait until the collection's finish. This way of ordering is also known as first-in-first-out (FIFO) (first-in, first-out). "First-come, first-served" is another term for it.

Operating systems employ a variety of queues to manage things within a system. The next job is frequently planned using a queuing approach in order to run programs as quickly as feasible while servicing the largest number of people possible. Furthermore, while we type, keystrokes might occasionally outweigh the text on the screen. The reason is that the computer is now engaged with other duties. The strokes of keys are queued in a file and eventually shown on the screen in the right sequence.

Different operations in Queue Data Structure:

The various operations that are supported by a queue data structure that helps the user to modify and manipulate the data present in the queue are:

  • Enqueue operation: The term "enqueue" refers to the act of adding a new element to a queue. Where does a new individual go and wait in a standard queue at a ticket counter to join the queue? The individual walks to the back of the room and takes a seat. A new element in a queue is similarly added at the end of the queue.
  • Dequeue operation: Dequeue is the process of deleting an item from a queue. We must delete the queue member that was put first since the queue follows the FIFO principle. We'll delete the front element and make the element behind it the new front element because the element added initially will naturally be at the head of the queue.
  • Front Operation: This works similarly to the peek operation in stacks in that it returns the value of the first element without deleting it.
  • isEmpty Operation: The isEmpty() function is used to check if the Queue is empty or not.
  • isFull Operation: The isFull() function is used to check if the Queue is full or not.

After seeing all the different valid operations for the queue data structure, let us write a code to implement all of these operations as functions in the desired programming language.

Let us begin with writing a basic program in Java programming language that will have the functions to simulate all the operations discussed above on the queue data structure.

Java Code:

Output:

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

Now let us have a look at the C++ code to implement Queue data structure.

C++ Code:

Output:

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

Now let us have a look at the C code to implement the Queue data structure.

C Code:

Output:

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

So, in this article, we understood the Queue data structure and various operations that we can do on the Queue data structure to modify and manipulate the data present in the Queue. And in the latter section of the article, we also have written code in three different programming languages (C, C++, and Java) where each operation of the Queue Data structure is implemented as an individual function that is called whenever we need to perform that particular operation on the data in the Queue.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA