Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Unit VI

Queue

Mrs. Manisha Desai


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
Descending).

Mrs. Manisha Desai


Queue Concept

Mrs. Manisha Desai


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“
• It is First In First Out Structure(FIFO)

The first one in the line is the first one to be Mrs. Manisha Desai

served
Queue Applications

• One of the most common data processing structures


• Queue is used in many applications such as as simulation,
priority queue, job queue etc
• Frequently used in most of the system software's like operating
systems, Network and Database implementations and in many
more other areas
• Very useful in time-sharing and distributed computer systems
where many widely distributed users share the system
simultaneously
Mrs. Manisha Desai
Implementation using array

Mrs. Manisha Desai


Implementation using Linked List

Mrs. Manisha Desai


Queue using Linked list

• To implement queue using linked list, we need to set the following things
before implementing actual operations.
• Step 1 - Include all the header files which are used in the program. And
declare all the user defined functions.
• Step 2 - Define a 'Node' structure with two members data and next.
• Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.
• Step 4 - Implement the main method by displaying Menu of list of
operations and make suitable function calls in the main method
to perform user selected operation.
Mrs. Manisha Desai
enQueue(value)-using Linked list

• We can use the following steps to insert a new node into the queue...
• Step 1 - Create a newNode with given value and set 'newNode →next'
to NULL.
• Step 2 - Check whether queue is Empty (rear == NULL)
• Step 3 - If it is Empty then,
set front = newNode and rear = newNode.
• Step 4 - If it is Not Empty then,set
rear→next = newNode and rear = newNode.
Mrs. Manisha Desai
deQueue()-using Linked list

• We can use the following steps to delete a node from the queue...
• Step 1 - Check whether queue is Empty (front == NULL).
• Step 2 - If it is Empty, then display "Queue is Empty!!! Deletion
is not possible!!!" and terminate from the function
• Step 3 - If it is Not Empty then, define a Node pointer 'temp' and
set it to 'front'.
• Step 4 - Then set 'front = front → next' and delete 'temp'
(free(temp)).

Mrs. Manisha Desai


display()

• We can use the following steps to display the elements (nodes) of a


queue...
• Step 1 - Check whether queue is Empty (front == NULL).
• Step 2 - If it is Empty then, display 'Queue is Empty!!!' and terminate
the function.
• Step 3 - If it is Not Empty then, define a Node pointer 'temp' and
initialize with front.
• Step 4 - Display 'temp → data --->' and move it to the next node. Repeat
the same until 'temp' reaches to 'rear' (temp → next != NULL).
• Step 5 - Finally! Display 'temp → data ---> NULL'.
Mrs. Manisha Desai
The Queue ADT

• The Queue ADT Stores arbitrary objects


• Insertion and deldtion follow the First In First Out Scheme
• Insertion at the rear of the queue And removal at the front of the
queue

Mrs. Manisha Desai


The Queue ADT-Queue Operations

• enqueue() − add (store) an item to the queue.


• dequeue() − remove (access) an item from the queue.
• Queue front()/peek() − Gets the element at the front of the
queue without removing it.
• Queue rear()
• isfull() − Checks if the queue is full.
• isempty() − Checks if the queue is empty.

Mrs. Manisha Desai


Algorithms

Algorithm isempty
Algorithm peek
begin procedure isempty
begin procedure peek if front is less than MIN OR front is greater than rear
return queue[front] return true
end procedure else
return false
endif

Algorithm isfull end procedure

begin procedure isfull


if rear equals to MAXSIZE
return true
else
return false
endif
end procedure
Mrs. Manisha Desai
Enqueue Operation

• Queues maintain two data pointers, front and rear.


• following steps should be taken to enqueue (insert)
data into a queue
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error
and exit.
Step 3 − If the queue is not full, increment rear procedure enqueue(data)
pointer to point the next empty space. if queue is full
return overflow
Step 4 − Add data element to the queue location, endif
where the rear is pointing. rear ← rear + 1
queue[rear] ← data
Step 5 − return success. return true
Mrs. Manisha Desai end procedure
Dequeue Operation

The following steps are taken to perform


dequeue operation −
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce
underflow error and exit.
Step 3 − If the queue is not empty, access the
data where front is pointing. procedure dequeue
Step 4 − Increment front pointer to point to if queue is empty
return underflow
the next available data element. end if
Step 5 − Return success. data = queue[front]
front ← front + 1
Mrs. Manisha Desai return true
end procedure
Multi-queues

• Problems with the queues represented with arrays are


• We have to allocate large space to avoid the overflow.
• Large space will be wasted due to less size of queue.
• There exits a trade-off between the number of overflows and the
space.
• One possibility to reduce this tradeoff is to represent more than
one queue in the same array of sufficient size

Mrs. Manisha Desai


Multi-queues

• Create a data structure kQueues that represents k queues.


Implementation of kQueues should use only one array, i.e., k
queues should use the same array for storing elements. Following
functions must be supported by kQueues.
• enqueue(int x, int qn) –> adds x to queue number ‘qn’ where qn is
from 0 to k-1
• dequeue(int qn) –> deletes an element from queue number ‘qn’
where qn is from 0 to k-1

Mrs. Manisha Desai


Divide the array in slots of size n/k

• A simple way to implement k queues is to divide the array in k slots of


size n/k each, and fix the slots for different queues, i.e., use arr[0] to
arr[n/k-1] for the first queue, and arr[n/k] to arr[2n/k-1] for queue2
where arr[] is the array to be used to implement two queues and size of
array be n.
• The problem with this method is an inefficient use of array space. An
enqueue operation may result in overflow even if there is space
available in arr[]. For example, consider k as 2 and array size n as 6. Let
we enqueue 3 elements to first and do not enqueue anything to the
second queue. When we enqueue the 4th element to the first queue,
there will be overflow even if we have space for 3 more elements in the
array.
Mrs. Manisha Desai
Why Circular Queue..?

• In a normal Queue Data Structure, we can insert elements until queue becomes full. But once
the queue becomes full, we can not insert the next element until all the elements are deleted
from the queue.

This situation also says that Queue is Full and we cannot insert the new element because 'rear' is
still at last position.
In the above situation, even though we have empty positions in the queue we can not make use of
them to insert the new element.
This is the major problem in a normal queue data structure. To overcome this problem we use a
circular queue data structure.
Mrs. Manisha Desai
Circular Queue

• A circular queue is a linear data structure in which the operations are


performed based on FIFO (First In First Out) principle and the last
position is connected back to the first position to make a circle.

Mrs. Manisha Desai


Circular Queue Example

Mrs. Manisha Desai


enQueue(value) - Inserting value into the
Circular Queue

• This function is used to insert an element into the circular queue. In a


circular queue, the new element is always inserted at Rear position.
• Steps:
1. Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) ||
(rear == front-1)).
2. If it is full then display Queue is full. If queue is not full then, check if
(rear == SIZE – 1 && front != 0) if it is true then set rear=0 and insert
element.

Mrs. Manisha Desai


deQueue() - Deleting a value from the Circular
Queue

• This function is used to delete an element from the circular queue. In a


circular queue, the element is always deleted from front position.
• Steps:
1. Check whether queue is Empty means check (front==-1).
2. If it is empty then display Queue is empty. If queue is not empty then
step 3
3. Check if (front==rear) if it is true then set front=rear= -1 else check if
(front==size-1), if it is true then set front=0 and return the element.

Mrs. Manisha Desai


display() - Displays the elements of a Circular
Queue

• Step 1 - Check whether queue is EMPTY. (front == -1)


• Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
• Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front'.
• Step 4 - Check whether 'front <= rear', if it is TRUE, then display 'queue[i]' value and
increment 'i' value by one (i++). Repeat the same until 'i <= rear' becomes FALSE.
• Step 5 - If 'front <= rear' is FALSE, then display 'queue[i]' value and increment 'i' value by
one (i++). Repeat the same until'i <= SIZE - 1' becomes FALSE.
• Step 6 - Set i to 0.
• Step 7 - Again display 'cQueue[i]' value and increment i value by one (i++). Repeat the
same until 'i <= rear' becomes FALSE.

Mrs. Manisha Desai


Advantages of Circular Queue

• Circular Queues offer a quick and clean way to store FIFO data
with a maximum size.
• Doesn’t use dynamic memory → No memory leaks
• Conserves memory as we only store up to our capacity (opposed to
a queue which could continue to grow if input outpaces output.)
• Simple Implementation → easy to trust and test
• Never has to reorganize / copy data around
• All operations occur in constant time O(1)
Mrs. Manisha Desai
Dequeue

• Deque or Double Ended Queue is a generalized version of Queue


data structure that allows insert and delete at both ends.

Mrs. Manisha Desai


Operations on Deque:

insertFront(): Adds an item at the front of Deque.


insertLast(): Adds an item at the rear of Deque.
deleteFront(): Deletes an item from front of Deque.
deleteLast(): Deletes an item from rear of Deque.
getFront(): Gets the front item from queue.
getRear(): Gets the last item from queue.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.

Mrs. Manisha Desai


Types of Deque
An input-restricted deque
is one where deletion can be made from both ends, but insertion can be made
at one end only.

An output-restricted deque
is one where insertion can be made at both ends, but deletion can be made
from one end only.

Mrs. Manisha Desai


Priority Queue

Priority Queue is an extension


of queue with following properties.
• Every item has a priority associated with
it.
• An element with high priority is
dequeued before an element with low
priority.
• If two elements have the same priority,
they are served according to their order
in the queue.

Mrs. Manisha Desai


Operations on priority queue

• insert(item, priority): Inserts an item with given priority.


• getHighestPriority(): Returns the highest priority item.
• deleteHighestPriority(): Removes the highest priority item.

Mrs. Manisha Desai


Types of Priority QUEUE

• ASCENDING/Min Priority Queue

• Items are entered arbitrarily & only the smallest item may be removed.
• If apq is an ascending priority queue, the operation pqinsert(apq,x) inserts element
x into apq and pqmindelete(apq) removes the minimum element from apq and
returns its value.
DESCENDING/Max Priority Queue

• Items are entered arbitrarily & only the largest item may be removed.
• A descending priority queue is similar but allows deletion of only the largest item.
• The operations applicable to a descending priority queue, dpq, are pqinsert(dpq,x)
and pqmaxdelete(dpq). pqinsert(dpq,x) inserts an element x into dpq and is
logically identical to pqinsert for an ascending priority queue. pqmaxdelete(dpq)
removes the maximum element from dpq and returns its value.

Mrs. Manisha Desai


Types of Priority QUEUE

Mrs. Manisha Desai


Case Study

• Priority queue in bandwidth management

Mrs. Manisha Desai

You might also like