0% found this document useful (0 votes)
45 views66 pages

UNIT-6 FDS Queue

Uploaded by

dusanesanika7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views66 pages

UNIT-6 FDS Queue

Uploaded by

dusanesanika7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 66

“QUEUE


Prepared By
Prof. Pravin R Pachorkar
(Assistant Professor)
Computer Dept.
CLASS : SE COMPUTER 2019
SUBJECT : FDS (SEM-I)
1
.UNIT : VI
Syllabus
 Basic concept
 Queue as Abstract Data Type
 Representation of Queue using Sequential
Organization
 Queue Operations
 Circular Queue and its advantages
 Multi-queues,Linked Queue and Operations
 Deque-Basic concept, types (Input restricted and Output
restricted)

 Priority Queue-Basic concept, types (Ascending and


Introduction of Queue
 “A queue is an ordered list in which all

insertions are done at one end, called rear and


deletions at another end called front.
 Queue is referred to be as First In First Out list.
 For example, people waiting in line for a rail

ticket form a queue.


 Queue when implemented using arrays has some

drawbacks which can be avoided by circular


queue

Introduction of Queue
 One of the most common data processing

structures
 Frequently used in most of the system
software's like operating systems, Network
and Database implementations and in many
more other areas

Fig : Normal/Regular Queue presentation


Introduction 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.
Basic Operations of Queue
Queue operations work as follows:

• Two pointers FRONT and REAR

• FRONT track the first element of the queue

• REAR track the last element of the queue

• initially, set value of FRONT and REAR to -1


Queue
1. Enqueue() – Adds (or stores) an element to the end of the queue..

2. Dequeue() – Removal of elements from the queue.

3. Peek() or front()- Acquires the data element available at the front


node of the queue without deleting it.
4. rear() – This operation returns the element at the rear end without
removing it.
5. isFull() – Validates if the queue is full.

6. isNull() – Checks if the queue is empty.


Representation of Queue
• Ways to implement the queue

• There are two ways of implementing the Queue:

1. Implementation using array: The sequential allocation in a Queue can


be implemented using an array.

2. Implementation using Linked list: The linked list allocation in a Queue


can be implemented using a linked list.
Queue using Array
Representation
1. Array Representation of Queue:
• 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.
Operation on queue
implemented using array
Operation on queue
implemented using array
Operation on queue
implemented using array

https://www.tutorialspoint.com/cplusplus-program-to-implement-que
Queue using Linked
Representation
Linked List Representation of Queue:

A queue can also be represented using following entities:

• Linked-lists,

• Pointers, and

• Structures.
Queue using Linked
Representation
Algorithm for Enqueue:-
• Return Queue full
• Else
• Rear+Rear+1
• Queue[Rear]=Data
• If(Front== -1)
• Front= 0

Algorithm for Dequeue:-


1. If(Front==-1||Front==Rear+1)
2. Return
3. Else
4. Queue[Front]=0
5. Front=Front+1
Queue Implementation using
Linked List
//function to enter elements in queue
void enqueue ( int value )
{
Node *ptr = new Node();
ptr->data= value;
ptr->link = NULL;

//If inserting the first element/node


if( front == NULL )
{
front = ptr;
rear = ptr;
}
else
{
rear ->link = ptr;
rear = ptr;
}
}
Queue Implementation using
Linked List
//function to delete/remove element from queue
void dequeue ( )
{
if( isempty() )
cout<<"Queue is empty\n";
else
//only one element/node in queue.
if( front == rear)
{
free(front);
front = rear = NULL;
}
else
{
Node *ptr = front;
front = front->link;
free(ptr);
}
}
Queue Implementation using
Linked List
//function to display queue
void displayQueue()
{
if (isempty())
cout<<"Queue is empty\n";
else
{
Node *ptr = front;
while( ptr !=NULL)
{
cout<<ptr->data<<" ";
ptr= ptr->link;
}
}
}
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.

5. Queues are used in operating systems for handling interrupts.


Types of Queue
A queue is a useful data structure in programming. It is similar to the ticket queue
outside a cinema hall, where the first person entering the queue is the first person
who gets the ticket.
There are four different types of queues:
Simple Queue
Circular Queue
Priority Queue
Double Ended Queue
Types of Queue
Types of Queue
Simple Queue
In a simple queue, insertion takes place at the rear and removal occurs at the front. It
strictly follows the FIFO (First in First out) rule.

Circular Queue
In a circular queue, the last element points to the first element making a circular link.
Types of Queue
Circular Queue :

The main advantage of a circular queue over a simple queue is better memory
utilization. If the last position is full and the first position is empty, we can insert an
element in the first position. This action is not possible in a simple queue.

Priority Queue

A priority queue is a special type of queue in which each element is associated with a
priority and is served according to its priority. If elements with the same priority
occur, they are served according to their order in the queue.
Types of Queue
Deque ( Double Ended Queue):

In a double ended queue, insertion and removal of elements can be performed from
either from the front or rear. Thus, it does not follow the FIFO (First In First Out)
rule.
Array representation and
implementation of queue
 An array representation of queue require three entities

:
1. An array to hold queue element
2. A variable to hold index of the front element
3. A variable to hold index of the rear element

Example of Queue using array :


https://www.tutorialspoint.com/cplusplus-program-to-implement-que
Array representation and
implementation of queue
Array representation and
implementation of queue
STACK VS QUEUE
Queue Example
Queue Example
Queue Example
Queue Example
Circular Queue
A Circular Queue is an extended version of a normal queue where the last
element of the queue is connected to the first element of the queue forming a
circle.

The operations are performed based on FIFO (First In First Out) principle. It
is also called ‘Ring Buffer’.

In a normal Queue, we can insert elements until queue becomes full. But once queue
becomes full, we can not insert the next element even if there is a space in front of
queue.
Operation on Circular
Queue
1. Front: Get the front item from the queue.
2. Rear: Get the last item from the queue.
3. enQueue(value) This function is used to insert an element into the
circular queue. In a circular queue, the new element is always inserted at
the rear position.
Check whether the queue is full – [i.e., the rear end is in just before the
front end in a circular manner].
If it is full then display Queue is full.
If the queue is not full then, insert an element at the end of the queue.
4. deQueue() This function is used to delete an element from the circular
queue. In a circular queue, the element is always deleted from the front
position.
Check whether the queue is Empty.
If it is empty then display Queue is empty.
If the queue is not empty, then get the last element and remove it from the
queue.
Operation on Circular
Queue
Illustration of Circular Queue Operations:
Operation on Circular
Queue
A circular queue can be implemented using two data structures:
• Array
• Linked List
Implement Circular Queue using
Array
1. Initialize an array queue of size n, where n is the maximum number of elements that
the queue can hold.
2. Initialize two variables front and rear to -1.
3. Enqueue: To enqueue an element x into the queue, do the following:
• Increment rear by 1.
If rear is equal to n, set rear to 0.
• If front is -1, set front to 0.
• Set queue[rear] to x.
4. Dequeue: To dequeue an element from the queue, do the following:
• Check if the queue is empty by checking if front is -1.
If it is, return an error message indicating that the queue is empty.
• Set x to queue[front].
• If front is equal to rear, set front and rear to -1.
• Otherwise, increment front by 1 and if front is equal to n, set front to 0.
Implement Circular Queue using
Linked
1. Asd
Implement Circular Queue using
Linked
void enqueue (int data)
{
Node *newNode = new Node ();
newNode->data = data;
newNode->next = nullptr;
if (front == nullptr)
{
front = rear = newNode;
rear->next = front;
}
else
{
rear->next = newNode;
rear = newNode;
rear->next = front;
}
cout << data << " has been enqueued." << endl;
}
Implement Circular Queue using
Linked
void dequeue ()
{
if (front == nullptr)
{
cout << "Queue is empty." << endl;
}
else if (front == rear)
{
Node *temp = front;
front = rear = nullptr;
delete temp;
}
else
{
Node *temp = front;
front = front->next;
rear->next = front;
delete temp;
}
}
Implement Circular Queue using
Linked
void display ()
{
if (front == nullptr)
{
cout << "Queue is empty." << endl;
}
else
{
Node *temp = front;
cout << "Circular Queue: ";
do
{
cout << temp->data << " ";
temp = temp->next;
}
while (temp != front);
cout << endl;
}
}
Circular Queue
DeQueue
Deque ( Double Ended Queue):

In a double ended queue, insertion and removal of elements can be performed from
either from the front or rear. Thus, it does not follow the FIFO (First In First Out)
rule.
• 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.
• The representation of the deque is shown in the below image -
Operation of DeQueue
1. Count() : This function is used to count the total number of elements in the deque
in data structure. This can be done by iterating over the whole deque in data
structure.

2. addFront() : This function is used to insert an element at the front end of the deque
in data structure.

3. addRear() : This function is used to insert element at the rear end of the deque in
data structure

4. delFront(): This function is used to delete elements from the front end of the deque
in data structure

5. delRear(): This function is used to delete elements from the rear end of the deque
in data structure
6. display(): This function is used to get or peek all the elements from the deque in
data structure
Types of Queue
There are two types of deque that are discussed as follows –

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.
Advantages of Deque
1. Dynamic Size: Deques can grow or shrink dynamically.

2. Efficient Operations: Deques provide efficient O(1) time complexity


for inserting and removing elements from both ends.

3. Versatile: Deques can be used as stacks (LIFO) or queues (FIFO), or


as a combination of both.

4. No Reallocation: Deques do not require reallocation of memory when


elements are inserted or removed.

5. Thread Safe: Deques can be thread-safe if used with proper


synchronization.
Disadvantages of Deque
1. Memory Overhead: Deques have higher memory overhead compared to
other data structures due to the extra pointers used to maintain the
double-ended structure.

2. Synchronization: Deques can cause synchronization issues if not used


carefully in multi-threaded environments.

3. Complex Implementation: Implementing a deque can be complex

4. Not All Platforms: Deques may not be supported by all platforms

5. Not Suitable for Sorting: Deques are not designed for sorting or
searching, as these operations require linear time.
6. Limited Functionality: Deques have limited functionality compared to
other data structures such as arrays, lists, or trees.
Applications of Deque
1. Deque can be used as both stack and queue, as it supports both
operations.

2. Deque can be used as a palindrome checker means that if we read the


string from both ends, the string would be the same.

3. It is used in job scheduling algorithms..

4. Graph traversal : Deques can be used to implement Breadth-First


Search (BFS) on a graph.

5. In a web browser’s history, recently visited URLs are added to the


front of the deque and the URL at the back of the deque is removed
after some specified number of operations of insertions at the front.
DeQueue as an ADT
Implementation of DeQueue using
linked list
Implementation of DeQueue using
linked list
1) To check Empty and Full queue for dequeue :
Implementation of DeQueue using
linked list
2) Insert at beginning(front) : 3) Insert at end(rear) :
Implementation of DeQueue using
linked list
4) Delete at beginning(front) : 5) Delete at end(rear) :
Priority Queue
• A priority queue is an abstract data type that behaves similarly to the
normal queue except that each element has some priority, i.e., the element
with the highest priority would come first in a priority queue. The priority
of the elements in a priority queue will determine the order in which
elements are removed from the priority queue.

• The priority queue supports only comparable elements, which means that
the elements are either arranged in an ascending or descending order.

• For example, suppose we have some values like 1, 3, 4, 8, 14, 22 inserted


in a priority queue with an ordering imposed on the values is from least to
the greatest. Therefore, the 1 number would be having the highest priority
while 22 will be having the lowest priority.
Example : https://youtu.be/1KRbIVZZa4
Priority Queue
Characteristics of a Priority queue :

• A priority queue is an extension of a queue that contains the


following characteristics:

• Every element in a priority queue has some priority associated


with it.

• An element with the higher priority will be deleted before the


deletion of the lesser priority.

• If two elements in a priority queue have the same priority, they


will be arranged using the FIFO principle.
Types of Priority Queue
There are two types of priority queue:
1. Ascending order priority queue:
2. Descending order priority queue:

Ascending order priority queue: In ascending order priority queue,


a lower priority number is given as a higher priority in a priority. For
example, we take the numbers from 1 to 5 arranged in an ascending
order like 1,2,3,4,5; therefore, the smallest number, i.e., 1 is given as
the highest priority in a priority queue.
Types of Priority Queue
There are two types of priority queue:
1. Ascending order priority queue:
2. Descending order priority queue:

Descending order priority queue: In descending order priority


queue, a higher priority number is given as a higher priority in a
priority. For example, we take the numbers from 1 to 5 arranged in
descending order like 5, 4, 3, 2, 1; therefore, the largest number, i.e.,
5 is given as the highest priority in a priority queue.
Priority Queue as an ADT
Priority Queue as an ADT

Priority as ADT : https://youtu.be/dFBoOS38dAk


Applications of Priority
Queue
1. It is used in the Dijkstra's shortest path algorithm.

2. It is used in prim's algorithm

3. It is used in data compression techniques like Huffman code.

4. It is used in heap sort.

5. It is also used in operating system like priority scheduling, load


balancing and interrupt handling.
Multiple Queue using array
Multi queue is data structure in which multiple queues are maintained. This
type of data structures are utilized for process scheduling. We might use
one dimensional array or multidimensional array to illustrated a multiple
queue.
Multiple Queue using array
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.

5. Queues are used in operating systems for handling interrupts.


Application of Queue
Application of Queue :
1. Fcfs Schedulling
2. Queue at Cashier counter

Applications of deque
1. Pallindrome checker.

Applications of circular queue


1. Traffic light functioning is the best example for circular queues. The
colors in the traffic light follow a circular pattern.
2. Round-robin schedulling

Application of Priority Queue :


3. Prims & Kruskals Algorithms
4. Dijkstra’s Shortest Path Algorithm
Application of Queue
Example of RR : https://youtu.be/hy5dn9mK36I

Process schedulling : https://youtu.be/THqcAa1bbFU

Priority schedulling : https://youtu.be/YzBBJYfwdi8


https://youtu.be/VSMAjMfJ6KQ

FCFS Schedulling : https://youtu.be/cNdEQKw4apM

Josphous PROBLEM : https://youtu.be/uCsD3ZGzMgE


References
1. https://www.javatpoint.com/ds-types-of-queues
2. https://www.programiz.com/dsa/types-of-queue
3. https://www.scaler.com/topics/dequeue-in-data-structure/
4. https://
www.simplilearn.com/tutorials/data-structure-tutorial/deque
ue-in-data-structure
5. https://
www.expertsmind.com/questions/implementation-of-multipl
e-queues-30152374.aspx
6. https://
www.codewhoop.com/queue/queue-using-linked-list.html
7. https://prepinsta.com/cpp-program/circular-queue-using-lin
ked-list
/
THANK YOU !!!!!

66 6
6

You might also like