SlideShare a Scribd company logo
Module II
Queue
Basic Concept of Queue
Queue:
A Queue is defined as a linear data structure that is open at both ends and the operations are performed
in First In First Out (FIFO) order.
Applications of Queue :
1.Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU.
2.Queues are used in asynchronous transfer of data (where data is not being transferred at the same rate
between two processes) for eg. pipes, file IO, sockets.
3.Queues are used as buffers in most of the applications like MP3 media player, CD player, etc.
4.Queue are used to maintain the play list in media players in order to add and remove the songs from the
play-list.
Module II
Queue
Basic Concept of Queue
FIFO Principle of Queue:
•A Queue is like a line waiting to purchase tickets, where the first person in line is the first
person served. (i.e. First come first serve).
•Position of the entry in a queue ready to be served, that is, the first entry that will be
removed from the queue, is called the front of the queue(sometimes, head of the queue),
similarly, the position of the last entry in the queue, that is, the one most recently added, is
called the rear (or the tail) of the queue. See the below figure
Module II
Queue
Basic Concept of Queue
Queue Representation:
Like stacks, Queues can also be represented in an array: In this representation, the Queue is
implemented using the array. Variables used in this case are
•Queue: the name of the array storing queue elements.
•Front: the index where the first element is stored in the array representing the queue.
•Rear: the index where the last element is stored in an array representing the queue.
We will be implementing the Queue data structure using an array in C+
+,Java and list in Python.
Characteristics of Queue:
•Queue can handle multiple data.
•We can access both ends.
•They are fast and flexible.
Module II
Queue
Queue as an ADT
Queue as an ADT:
The queue abstract data type is defined by the following structure and
operations. A queue is structured, as described above, as an ordered
collection of items which are added at one end, called the “rear,” and
removed from the other end, called the “front.” Queues maintain a FIFO
ordering property.
• Queue() : creates a new queue that is empty. It needs no parameters and returns an empty queue.
• enqueue(item): adds a new item to the rear of the queue. It needs the item and returns nothing.
• dequeue(): removes the front item from the queue. It needs no parameters and returns the item.
The queue is modified.
• isEmpty(): tests to see whether the queue is empty. It needs no parameters and returns a Boolean
value.
• size(): returns the number of items in the queue. It needs no parameters and returns an integer.
Module II
Queue
Types of Queue
Types of Queue :
There are four different types of queue that are listed as follows -
oSimple Queue or Linear Queue
oCircular Queue
oPriority Queue
oDouble Ended Queue (or Deque)
Simple Queue or Linear Queue :
In Linear Queue, an insertion takes place from one end while the deletion occurs from another end. The
end at which the insertion takes place is known as the rear end, and the end at which the deletion takes
place is known as front end. It strictly follows the FIFO rule.
The major drawback of using a linear Queue is that insertion is done only from the rear end. If the first
three elements are deleted from the Queue, we cannot insert more elements even though the space is
available in a Linear Queue. In this case, the linear Queue shows the overflow condition as the rear is
pointing to the last element of the Queue.
Module II
Queue
Types of Queue
Circular Queue :
In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue
except that the last element of the queue is connected to the first element. It is also known as
Ring Buffer, as all the ends are connected to another end. The representation of circular
queue is shown in the below image -
The drawback that occurs in a linear queue is overcome by using the circular queue. If the
empty space is available in a circular queue, the new element can be added in an empty
space by simply incrementing the value of rear. The main advantage of using the circular
queue is better memory utilization.
Module II
Queue
Types of Queue
Priority Queue:
It is a special type of queue in which the elements are arranged based on the priority. It is a
special type of queue data structure in which every element has a priority associated with it.
Suppose some elements occur with the same priority, they will be arranged according to the
FIFO principle. The representation of priority queue is shown in the below image -
Insertion in priority queue takes place based on the arrival, while deletion in the priority
queue occurs based on the priority. Priority queue is mainly used to implement the CPU
scheduling algorithms.
Module II
Queue
Types of Queue
Two types of priority queue : –
•Ascending priority queue - In ascending priority queue, elements can be inserted in
arbitrary order, but only smallest can be deleted first. Suppose an array with elements 7,
10,11,6 and 2 in the same order, so, insertion can be done with the same sequence, but the
order of deleting the elements is 2, 6, 7,10,11.
•Descending priority queue - In descending priority queue, elements can be inserted in
arbitrary order, but only the largest element can be deleted first. Suppose an array with
elements 8,6,10,9 and 7 in the same order, so, insertion can be done with the same sequence,
but the order of deleting the elements is 10,9,8,7,6.
Module II
Queue
Types of Queue
Deque (or, Double Ended Queue):
In Deque or Double Ended Queue, insertion and deletion can be done from both
ends of the queue either from the front or rear. It means that we can insert and
delete elements from both front and rear ends of the queue. Deque can be used as a
palindrome checker means that if we read the string from both ends, then the string
would be the same.
Deque can be used both as stack and queue as it allows the insertion and deletion
operations on both ends. Deque can be considered as stack because stack follows
the LIFO (Last In First Out) principle in which insertion and deletion both can be
performed only from one end. And in deque, it is possible to perform both insertion
and deletion from one end, and Deque does not follow the FIFO principle.
Module II
Queue
Types of Queue
Two types of deque :
•Input restricted deque - As the name implies, in input restricted queue, insertion operation
can be performed at only one end, while deletion can be performed from both ends.
•Output restricted deque - As the name implies, in output restricted queue, deletion
operation can be performed at only one end, while insertion can be performed from both
ends.
Module II
Queue
Operation
Operations performed on queue :
oEnqueue: The Enqueue operation is used to insert the element at the rear end of the queue.
It returns void.
oDequeue: It performs the deletion from the front-end of the queue. It also returns the
element which has been removed from the front-end. It returns an integer value.
oPeek: This is the third operation that returns the element, which is pointed by the front
pointer in the queue but does not delete it.
oQueue overflow (isfull): It shows the overflow condition when the queue is completely
full.
oQueue underflow (isempty): It shows the underflow condition when the Queue is empty,
i.e., no elements are in the Queue.
Ways to implement the queue:
•Implementation using array: The sequential allocation in a Queue can be implemented using an array.
•Implementation using Linked list: The linked list allocation in a Queue can be implemented using a
linked list.
Module II
Queue
Types of Queue
• Enqueue() Operation :
The enqueue() is a data manipulation operation that is used to insert elements into the stack. The following algorithm
describes the enqueue() operation in a simpler way.
Algorithm:
1 − START
2 – Check if the queue is full.
3 − If the queue is full, produce overflow error and exit.
4 − If the queue is not full, increment rear pointer to point the next empty space.
5 − Add data element to the queue location, where the rear is pointing.
6 − return success.
7 – END
Module II
Queue
Operation
• Dequeue() Operation:
The dequeue() is a data manipulation operation that is used to remove elements from the stack. The following algorithm
describes the dequeue() operation in a simpler way.
Algorithm:
1 – START
2 − Check if the queue is empty.
3 − If the queue is empty, produce underflow error and exit.
4 − If the queue is not empty, access the data where front is pointing.
5 − Increment front pointer to point to the next available data element.
6 − Return success.
7 – END
Module II
Queue
Operation
• peek() Operation:
The peek() is an operation which is used to retrieve the frontmost element in the queue, without deleting it. This
operation is used to check the status of the queue with the help of the pointer.
Algorithm:
1 – START
2 – Return the element at the front of the queue
3 – END
• isFull() Operation:
The isFull() operation verifies whether the stack is full.
Algorithm:
1 – START
2 – If the count of queue elements equals the queue size, return true
3 – Otherwise, return false
4 – END
• isEmpty() operation:
The isEmpty() operation verifies whether the stack is empty. This operation is used to check the status of the stack with
the help of top pointer.
Algorithm:
1 – START
2 – If the count of queue elements equals zero, return true
3 – Otherwise, return false
4 – END
Module II
Queue
Operation
Advantages of Queue:
•A large amount of data can be managed efficiently with ease.
•Operations such as insertion and deletion can be performed with ease as it
follows the first in first out rule.
•Queues are useful when a particular service is used by multiple consumers.
•Queues are fast in speed for data inter-process communication.
•Queues can be used in the implementation of other data structures.
Disadvantages of Queue:
•The operations such as insertion and deletion of elements from the middle are
time consuming.
•Limited Space.
•In a classical queue, a new element can only be inserted when the existing
elements are deleted from the queue.
•Searching an element takes O(N) time.
•Maximum size of a queue must be defined prior.

More Related Content

Similar to The Queue in Data structure and algorithm (20)

Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Queues
Queues Queues
Queues
nidhisatija1
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
MeghaKulkarni27
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
Hanif Durad
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
VeerannaKotagi1
 
Queue and its operations
Queue and its operationsQueue and its operations
Queue and its operations
V.V.Vanniaperumal College for Women
 
Data structure
Data structureData structure
Data structure
ashutoshsingh1011
 
DS ppt1.pptx.c programing. Engineering. Data structure
DS ppt1.pptx.c programing. Engineering. Data structureDS ppt1.pptx.c programing. Engineering. Data structure
DS ppt1.pptx.c programing. Engineering. Data structure
dibyajyotijena05
 
DS10-QUEUE0000000000000000000000000000000000000.pptx
DS10-QUEUE0000000000000000000000000000000000000.pptxDS10-QUEUE0000000000000000000000000000000000000.pptx
DS10-QUEUE0000000000000000000000000000000000000.pptx
ProfVMGawde
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
9f556226-babd-4276-b964-371c6a5a77b9.pdf
9f556226-babd-4276-b964-371c6a5a77b9.pdf9f556226-babd-4276-b964-371c6a5a77b9.pdf
9f556226-babd-4276-b964-371c6a5a77b9.pdf
kumarharsh2119hk
 
GROUP2.pptxfdfffffffffffffffffffffffffffffffffffffffffff
GROUP2.pptxfdfffffffffffffffffffffffffffffffffffffffffffGROUP2.pptxfdfffffffffffffffffffffffffffffffffffffffffff
GROUP2.pptxfdfffffffffffffffffffffffffffffffffffffffffff
binsyozane
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
Rojan Pariyar
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
ArmanKhan382533
 
Queue
QueueQueue
Queue
Raj Sarode
 
Basic Terminologies of Queue...Basic operations on Queue
Basic Terminologies of Queue...Basic operations on QueueBasic Terminologies of Queue...Basic operations on Queue
Basic Terminologies of Queue...Basic operations on Queue
arpitasen55
 
Queue
QueueQueue
Queue
Allana Institute of management sciences
 
Queue-Data Structure
Queue-Data StructureQueue-Data Structure
Queue-Data Structure
Paurav Shah
 
Queue data structures and operation on data structures
Queue data structures and operation on data structuresQueue data structures and operation on data structures
Queue data structures and operation on data structures
muskans14
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
MeghaKulkarni27
 
DS ppt1.pptx.c programing. Engineering. Data structure
DS ppt1.pptx.c programing. Engineering. Data structureDS ppt1.pptx.c programing. Engineering. Data structure
DS ppt1.pptx.c programing. Engineering. Data structure
dibyajyotijena05
 
DS10-QUEUE0000000000000000000000000000000000000.pptx
DS10-QUEUE0000000000000000000000000000000000000.pptxDS10-QUEUE0000000000000000000000000000000000000.pptx
DS10-QUEUE0000000000000000000000000000000000000.pptx
ProfVMGawde
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
9f556226-babd-4276-b964-371c6a5a77b9.pdf
9f556226-babd-4276-b964-371c6a5a77b9.pdf9f556226-babd-4276-b964-371c6a5a77b9.pdf
9f556226-babd-4276-b964-371c6a5a77b9.pdf
kumarharsh2119hk
 
GROUP2.pptxfdfffffffffffffffffffffffffffffffffffffffffff
GROUP2.pptxfdfffffffffffffffffffffffffffffffffffffffffffGROUP2.pptxfdfffffffffffffffffffffffffffffffffffffffffff
GROUP2.pptxfdfffffffffffffffffffffffffffffffffffffffffff
binsyozane
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
Rojan Pariyar
 
Basic Terminologies of Queue...Basic operations on Queue
Basic Terminologies of Queue...Basic operations on QueueBasic Terminologies of Queue...Basic operations on Queue
Basic Terminologies of Queue...Basic operations on Queue
arpitasen55
 
Queue-Data Structure
Queue-Data StructureQueue-Data Structure
Queue-Data Structure
Paurav Shah
 
Queue data structures and operation on data structures
Queue data structures and operation on data structuresQueue data structures and operation on data structures
Queue data structures and operation on data structures
muskans14
 

Recently uploaded (20)

Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
sebastianku31
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
Cloud Computing storage saas iaas paas.pptx
Cloud Computing storage saas iaas paas.pptxCloud Computing storage saas iaas paas.pptx
Cloud Computing storage saas iaas paas.pptx
viratkohli82222
 
Artificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowyArtificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowy
dominikamizerska1
 
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
Journal of Soft Computing in Civil Engineering
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
cloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.pptcloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.ppt
viratkohli82222
 
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Mohamed905031
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Class-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdfClass-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdf
takisvlastos
 
Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)
kevig
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
sebastianku31
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
Cloud Computing storage saas iaas paas.pptx
Cloud Computing storage saas iaas paas.pptxCloud Computing storage saas iaas paas.pptx
Cloud Computing storage saas iaas paas.pptx
viratkohli82222
 
Artificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowyArtificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowy
dominikamizerska1
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
cloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.pptcloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.ppt
viratkohli82222
 
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Mohamed905031
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Class-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdfClass-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdf
takisvlastos
 
Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)
kevig
 
Ad

The Queue in Data structure and algorithm

  • 1. Module II Queue Basic Concept of Queue Queue: A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order. Applications of Queue : 1.Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU. 2.Queues are used in asynchronous transfer of data (where data is not being transferred at the same rate between two processes) for eg. pipes, file IO, sockets. 3.Queues are used as buffers in most of the applications like MP3 media player, CD player, etc. 4.Queue are used to maintain the play list in media players in order to add and remove the songs from the play-list.
  • 2. Module II Queue Basic Concept of Queue FIFO Principle of Queue: •A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served. (i.e. First come first serve). •Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the queue, is called the front of the queue(sometimes, head of the queue), similarly, the position of the last entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the queue. See the below figure
  • 3. Module II Queue Basic Concept of Queue Queue Representation: Like stacks, Queues can also be represented in an array: In this representation, the Queue is implemented using the array. Variables used in this case are •Queue: the name of the array storing queue elements. •Front: the index where the first element is stored in the array representing the queue. •Rear: the index where the last element is stored in an array representing the queue. We will be implementing the Queue data structure using an array in C+ +,Java and list in Python. Characteristics of Queue: •Queue can handle multiple data. •We can access both ends. •They are fast and flexible.
  • 4. Module II Queue Queue as an ADT Queue as an ADT: The queue abstract data type is defined by the following structure and operations. A queue is structured, as described above, as an ordered collection of items which are added at one end, called the “rear,” and removed from the other end, called the “front.” Queues maintain a FIFO ordering property. • Queue() : creates a new queue that is empty. It needs no parameters and returns an empty queue. • enqueue(item): adds a new item to the rear of the queue. It needs the item and returns nothing. • dequeue(): removes the front item from the queue. It needs no parameters and returns the item. The queue is modified. • isEmpty(): tests to see whether the queue is empty. It needs no parameters and returns a Boolean value. • size(): returns the number of items in the queue. It needs no parameters and returns an integer.
  • 5. Module II Queue Types of Queue Types of Queue : There are four different types of queue that are listed as follows - oSimple Queue or Linear Queue oCircular Queue oPriority Queue oDouble Ended Queue (or Deque) Simple Queue or Linear Queue : In Linear Queue, an insertion takes place from one end while the deletion occurs from another end. The end at which the insertion takes place is known as the rear end, and the end at which the deletion takes place is known as front end. It strictly follows the FIFO rule. The major drawback of using a linear Queue is that insertion is done only from the rear end. If the first three elements are deleted from the Queue, we cannot insert more elements even though the space is available in a Linear Queue. In this case, the linear Queue shows the overflow condition as the rear is pointing to the last element of the Queue.
  • 6. Module II Queue Types of Queue Circular Queue : In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue except that the last element of the queue is connected to the first element. It is also known as Ring Buffer, as all the ends are connected to another end. The representation of circular queue is shown in the below image - The drawback that occurs in a linear queue is overcome by using the circular queue. If the empty space is available in a circular queue, the new element can be added in an empty space by simply incrementing the value of rear. The main advantage of using the circular queue is better memory utilization.
  • 7. Module II Queue Types of Queue Priority Queue: It is a special type of queue in which the elements are arranged based on the priority. It is a special type of queue data structure in which every element has a priority associated with it. Suppose some elements occur with the same priority, they will be arranged according to the FIFO principle. The representation of priority queue is shown in the below image - Insertion in priority queue takes place based on the arrival, while deletion in the priority queue occurs based on the priority. Priority queue is mainly used to implement the CPU scheduling algorithms.
  • 8. Module II Queue Types of Queue Two types of priority queue : – •Ascending priority queue - In ascending priority queue, elements can be inserted in arbitrary order, but only smallest can be deleted first. Suppose an array with elements 7, 10,11,6 and 2 in the same order, so, insertion can be done with the same sequence, but the order of deleting the elements is 2, 6, 7,10,11. •Descending priority queue - In descending priority queue, elements can be inserted in arbitrary order, but only the largest element can be deleted first. Suppose an array with elements 8,6,10,9 and 7 in the same order, so, insertion can be done with the same sequence, but the order of deleting the elements is 10,9,8,7,6.
  • 9. Module II Queue Types of Queue Deque (or, Double Ended Queue): In Deque or Double Ended Queue, insertion and deletion can be done from both ends of the queue either from the front or rear. It means that we can insert and delete elements from both front and rear ends of the queue. Deque can be used as a palindrome checker means that if we read the string from both ends, then the string would be the same. Deque can be used both as stack and queue as it allows the insertion and deletion operations on both ends. Deque can be considered as stack because stack follows the LIFO (Last In First Out) principle in which insertion and deletion both can be performed only from one end. And in deque, it is possible to perform both insertion and deletion from one end, and Deque does not follow the FIFO principle.
  • 10. Module II Queue Types of Queue Two types of deque : •Input restricted deque - As the name implies, in input restricted queue, insertion operation can be performed at only one end, while deletion can be performed from both ends. •Output restricted deque - As the name implies, in output restricted queue, deletion operation can be performed at only one end, while insertion can be performed from both ends.
  • 11. Module II Queue Operation Operations performed on queue : oEnqueue: The Enqueue operation is used to insert the element at the rear end of the queue. It returns void. oDequeue: It performs the deletion from the front-end of the queue. It also returns the element which has been removed from the front-end. It returns an integer value. oPeek: This is the third operation that returns the element, which is pointed by the front pointer in the queue but does not delete it. oQueue overflow (isfull): It shows the overflow condition when the queue is completely full. oQueue underflow (isempty): It shows the underflow condition when the Queue is empty, i.e., no elements are in the Queue. Ways to implement the queue: •Implementation using array: The sequential allocation in a Queue can be implemented using an array. •Implementation using Linked list: The linked list allocation in a Queue can be implemented using a linked list.
  • 12. Module II Queue Types of Queue • Enqueue() Operation : The enqueue() is a data manipulation operation that is used to insert elements into the stack. The following algorithm describes the enqueue() operation in a simpler way. Algorithm: 1 − START 2 – Check if the queue is full. 3 − If the queue is full, produce overflow error and exit. 4 − If the queue is not full, increment rear pointer to point the next empty space. 5 − Add data element to the queue location, where the rear is pointing. 6 − return success. 7 – END
  • 13. Module II Queue Operation • Dequeue() Operation: The dequeue() is a data manipulation operation that is used to remove elements from the stack. The following algorithm describes the dequeue() operation in a simpler way. Algorithm: 1 – START 2 − Check if the queue is empty. 3 − If the queue is empty, produce underflow error and exit. 4 − If the queue is not empty, access the data where front is pointing. 5 − Increment front pointer to point to the next available data element. 6 − Return success. 7 – END
  • 14. Module II Queue Operation • peek() Operation: The peek() is an operation which is used to retrieve the frontmost element in the queue, without deleting it. This operation is used to check the status of the queue with the help of the pointer. Algorithm: 1 – START 2 – Return the element at the front of the queue 3 – END • isFull() Operation: The isFull() operation verifies whether the stack is full. Algorithm: 1 – START 2 – If the count of queue elements equals the queue size, return true 3 – Otherwise, return false 4 – END • isEmpty() operation: The isEmpty() operation verifies whether the stack is empty. This operation is used to check the status of the stack with the help of top pointer. Algorithm: 1 – START 2 – If the count of queue elements equals zero, return true 3 – Otherwise, return false 4 – END
  • 15. Module II Queue Operation Advantages of Queue: •A large amount of data can be managed efficiently with ease. •Operations such as insertion and deletion can be performed with ease as it follows the first in first out rule. •Queues are useful when a particular service is used by multiple consumers. •Queues are fast in speed for data inter-process communication. •Queues can be used in the implementation of other data structures. Disadvantages of Queue: •The operations such as insertion and deletion of elements from the middle are time consuming. •Limited Space. •In a classical queue, a new element can only be inserted when the existing elements are deleted from the queue. •Searching an element takes O(N) time. •Maximum size of a queue must be defined prior.