UNIT-6 FDS Queue
UNIT-6 FDS 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)
structures
Frequently used in most of the system
software's like operating systems, Network
and Database implementations and in many
more other areas
• 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:
• 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
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.
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
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.
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.
• The priority queue supports only comparable elements, which means that
the elements are either arranged in an ascending or descending order.
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.
Applications of deque
1. Pallindrome checker.
66 6
6