0% found this document useful (0 votes)
6 views

Unit Three

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit Three

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Unit Three

Queue
What is Queue?
Queue is a linear data structure in which the insertion and deletion operations are
performed at two different ends. The insertion is performed at one end and deletion
is performed at another end. In a queue data structure, the insertion operation is
performed at a position which is known as ‘rear’. In queue data structure, the
insertion and deletion operations are performed based on FIFO (First In First Out)
principle.

A real world example of queue can be a single lane one way road, where the vehicle
enters first, exits first.
Queue as an ADT
A queue q of type T is a finite sequence of elements with the operations:
1. MakeEmpty(q): To make a as an empty queue
2. IsEmpty(q): To check whether the queue q is empty. Return true if q is empty,
return false otherwise
3. IsFull(q): To check whether the queue q is full. Return true in q is full, return
false otherwise
4. Enqueue(q,x): To insert an item x at the rear of the queue, if and only if q is not
full.
5. Dequeue(q): To delete an item from the front of the queue q, if and only if q is
not empty.
6. Traverse(q): To read entire queue that displays the content of the queue.
Thus, by using a queue we can perform above operations where a queue acts as an
ADT.
Operations on Queue
A queue is an object or more specifically an abstract data structure (ADT) that
allows the following operations:
1. Enqueue or insertion which inserts an element at the end of the queue
2. Dequeue or deletion which deletes an element at the start of the queue
Two pointers called FRONT and REAR are used to keep track of the elements in the
queue. When initializing the queue, we set the value of FRONT and REAR to 0.
On enqueuing an element, we increase the value of REAR index and place the new
element in the position pointed to by REAR. Before enqueuing, we check if queue
is already full or not.
On dequeuing an element, we return the value pointed to by FRONT and increase
the FRONT index. Before dequeuing, we check if queue is already empty or not.
When dequeuing the last element, we reset the values of FRONT and REAR to 0.
Circular Queue
• As we know that in a queue, the front pointer points to the first element while the
rear pointer points to the last element of the queue. The problem that arises with
the linear queue is that if some empty cells occur at the beginning of the queue
then we cannot insert new element at the empty space as the rear cannot be further
incremented.
• A circular queue is also a linear data structure like a normal queue that follows the
FIFO principle but it does not end the queue; it connects the last position of the
queue to the first position of the queue. If we want to insert new elements at the
beginning of the queue, we can insert it using the circular queue data structure.
• In the circular queue, when the rear reaches the end of the queue, then rear is reset
to zero. It helps in refilling all the free spaces. The problem of managing the
circular queue is overcome if the first position of the queue comes after the last
position of the queue.
Operations on Circular Queue
The following are the two operations that can be performed on a circular queue are:
• Enqueue: It inserts an element in a queue. The given below are the scenarios that can
be considered while inserting an element:
• If the queue is empty, then the front and rear are set to 0 to insert a new element.
• If queue is not empty, then the value of the rear gets incremented.
• If queue is not empty and rear is equal to n-1, then rear is set to 0.
• Dequeue: It performs a deletion operation in the Queue. The following are the points
or cases that can be considered while deleting an element:
• If there is only one element in a queue, after the dequeue operation is performed
on the queue, the queue will become empty. In this case, the front and rear values
are set to -1.
• If the value of the front is equal to n-1, after the dequeue operation is performed,
the value of the front variable is set to 0.
• If either of the above conditions is not fulfilled, then the front value is
incremented.
Example:
Let us consider a linear queue status as follows:

Next insert another element, say 66 to the queue. We cannot insert 66 to the queue
as the rear crossed the maximum size of the queue i.e. 5. There will be queue full
signal. The queue status is as follows:
This difficulty can be overcome if we treat queue position with index zero as a
position that comes after position with index four then we treat the queue as a
circular queue. In circular queue if we reach the end for inserting elements to it, it is
possible to insert new elements if the slots at the beginning of the circular queue are
empty.
Representation of Circular Queue
Let us consider a circular queue, which can hold maximum (MAX) of six elements.
Initially the queue is empty.

Now, insert 11 to the circular queue. Then circular queue status will be:
Insert new elements 22, 33, 44 and 55 into the circular queue. The circular queue
status is:

Now, delete an element. The element deleted is the element at the front of the
circular queue. So 11 is deleted. The circular queue status is as follows:
Again, delete an element. The element to be deleted is always pointed to by the
FRONT pointer. So 22 is deleted. The circular queue status is as follows:

Again, insert another element 66 to the circular queue. The status of the circular
queue is:
Now, insert new elements 77 and 88 into the circular queue. The circular queue
status is:

Now, if we insert an element to the circular queue, as COUNT = MAX we cannot


add the element to circular queue. So the circular queue is full.
Deque
In the preceding section, we saw that a queue in which we insert items at one end
and from which we remove items at the other end. In this section, we examine an
extension of the queue, which provides a means to insert and remove items at both
ends of the queue. This data structure is a deque. The word dequeue is an acronym
derived from double-ended queue. Below figure shows the representation of a
deque.
There are two variations of deque. They are:
1. Input restricted deque (IRD)
2. Output restricted deque (ORD)

An input restricted deque is a deque which allows insertions at one end but allows
deletions at both ends of the list. An output restricted deque is a deque which allows
deletions at one end but allows insertions at both ends of the list.
Priority Queue
• Priority Queue is an abstract data type that performs operations on data elements
per their priority. That is, higher priority elements are served first.
• However, if elements with the same priority occur, they are served according to
their order in the queue.
Assigning Priority Value
• Generally, the value of the element itself is considered for assigning the priority.
For example,
• The element with the highest value is considered the highest priority element.
However, in other cases, we can assume the element with the lowest value as the
highest priority element.
• We can also set priorities according to our needs.
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.
Priority Queue as ADT

A ascending priority queue of elements of type T is a finite sequence of


elements of T together with the operations:
● MakeEmpty(p): Create an empty priority queue p
● Empty(p): Determine if the priority queue p is empty or not
● Insert(p,x): Add element x on the priority queue p
● DeleteMin(p): If the priority queue p is not empty, remove the
minimum element of the queue and return it.
● FindMin(p): Retrieve the minimum element of the priority queue p.
Types of Priority Queue
There are two types of 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.
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.
Array Implementation of Priority Queue

Unordered array implementation:


● To insert an item, insert it at the rear end of the queue.
● To delete an item, find the position of the minimum element and
○ Either mark it as deleted (lazy deletion) or
○ Shift all elements past the deleted element by one position and then
decrement rear.
• Array implementation of priority queue:
Unordered array implementation:
Ordered array implementation:
● Set the front as the position of the smallest element and the rear as the position of
the largest element.
● To insert an element, locate the proper position of the new element and shift
preceding or succeeding elements by one position.
● To delete the minimum element, increment the front position.
Priority Queue Operations

● Declaration:
Data type of Priority Queue is the same as the Non-priority Queue.
#define MAXQUEUE 10 /* size of the queue items*/
struct pqueue { struct item{
int front; int value;
int rear; int priority;
struct item items[MAXQUEUE]; }
};
struct pqueue *pq;
● Insertion:
The insertion in Priority queues is the same as in non-priority queues.

● Deletion :
Deletion requires a search for the element of highest priority and deletes the
element with highest priority.
The following methods can be used for deletion/removal from a given Priority
Queue:
○ An empty indicator replaces deleted elements.
○ After each deletion elements can be moved up in the array decrementing the
rear.
○ The array in the queue can be maintained as an ordered circular array
Applications
● Resource Scheduling algorithm
● Sorting of a file
● CPU Scheduling algorithm

You might also like