Visual Studio
A queue is a linear data structure that consists of elements arranged in a sequential order where one end is used to add elements, and another for removing them which results in the FIFO (First-In First-Out) order of operations.
The queue is a linear data structure that has the following properties:-
- It works on the principle of FIFO (First In First Out)
- It has mainly 2 pointers to perform operations: Front & Rear. The Rear is for insertion and the Front is for deletion
- It is a linear data structure that gives sequential one-way access to the elements. Even though we can implement it using an array, we cannot access any element through
- indices directly because access to elements is given only through the rear and front pointers.
Applications of Queue: Due to its FIFO nature, queue is a widely utilised data structure in a variety of real world applications :-
-
Computer Science Fundamentals: its utilised in mostly the core operating system concepts like Job Scheduling Algorithms, Memory management for processing, basically putting the processes inside a queue so that they can be executed concurrently by the processor
-
Algorithms: To store addresses of tree nodes, linked list nodes, and Graph Nodes BFS (Breadth First Search) etc.
-
Real World Application: In a music or video player app you must've seen the option "Add to Queue" which is basically the Enqueue operation
This concept was demonstrated using a singke code that shows queuing, dequeuing and enqueuing in C++
Algorithm: Implement Queue using Array
- Start
- Define a class
Queue
with: An integer arrayarr[SIZE]
. Two integer variablesfront
andrear
initialized to-1
. Functions:enqueue()
,dequeue()
, anddisplay()
. - Enqueue operation (
enqueue(value)
)- If
rear == SIZE - 1
, display "Queue Overflow!" and return. - If
front == -1
, setfront = 0
. - Increment
rear
and insert the value intoarr[rear]
. - Display confirmation message.
- If
- Dequeue operation (
dequeue()
)- If
front == -1
ORfront > rear
, display "Queue Underflow!" and return. - Display the element
arr[front]
being removed. - Increment
front
.
- If
- Display operation (
display()
)- If
front == -1
ORfront > rear
, display "Queue is empty". - Otherwise, print all elements from
arr[front]
toarr[rear]
.
- If
- In
main()
- Create an object
q
of classQueue
. - Perform enqueue, dequeue, and display operations to test queue behavior.
- Create an object
- End
Through the above code the concept of queuing and its applications was seen in C++.