You are on page 1of 11

Data Structures & Algorithms

Queues
Session II

Dr. V.Umadevi M.Sc(CS &IT). M.Tech (IT)., M.Phil., PhD., D.Litt.,


Director, Department of Computer Science, Jairams Arts and
Science College, Karur.

1
Data Structures & Algorithms

Introduction to Queues Concepts

• Queue
• Application of Queue
• Types of Queue
• Basic Operations of Queue

2
Data Structures & Algorithms
Queue
• Queue is defined as a waiting line for a specific task,
For e.g.,
People waiting to purchase tickets
The first person in line is the first person served.

• Def -A queue is an ordered list


– rear - all insertions take place at one end
– Front- all deletion takes place at other end

• Work on the concept of First In First Out (FIFO).

• The simplest scheme employs a one-dimensional array and two


variables ‘front’ and ‘rear’.
– front is less than the position of the first element in the queue
– rear is the position of the last element in the queue

3
Data Structures & Algorithms
Application of Queue

• job scheduling. i.e. in a time sharing system, programs form a queue,


waiting to be executed.

• A line at a ticket reservation counter

• A line in a bank

Types of Queues

• Circular Queue

• Deque

• Priority Queue

4
Data Structures & Algorithms

Circular Queue

• This technique essentially allows the queue to wrap around upon reaching
the end of the array.

• Large amount of memory require.

• A Circular queue may be specified by a single pointer p to the list, node(p)


is the rear of the queue and the following the node is its front.

• To insert element in the rear of a circular queue, the element is inserted


into the front of the queue and the circular queue and the
circular list pointer is then advanced one element so that the
new element becomes the rear.

5
Data Structures & Algorithms

Dequeue
• Elements can be added or removed at either end but not in middle
(deque means double ended queue).

• Deque is maintained by a circular array.

• Deque with pointers Left and Right will point to the two ends of the
deque.

• Types of Deque:
– Input restricted deque - which allows insertion at only one end of
the list but allows deletion at both ends of the list.
– Output restricted deque - which allows deletion at only one end of
the list but allows insertion at both ends of the list.

6
Data Structures & Algorithms

Priority Queue

Def - A queue which holds items that are sorted such that the highest priority
Item is always the next one to be retrieved.
• A Priority Queue is a collection of elements
• Each element has been assigned a priority and based on the order in
which elements are deleted and processed from the rules specified
below:
– A higher priority element is processed before any lower priority element.
– Any two elements having same priority will be accessed according to the order
in which they are added in the queue.

• Items added to this queue have a priority associated with them:


this priority determines the order in which they exit from the queue

– i.e., the highest priority items are removed first.

7
Data Structures & Algorithms
Basic Queue Operations

Queue has basically three primitive operations are follows:

• Insert (q, x) - insert an item x in the rear of the queue q.


For Insert operation there no limit for the number of
elements.

• Remove (q, x)- delete the element from the queue q and set x to its
contents. This operation can be applied only if the
queue is not empty.

• Empty (q)- returns true or false depending on whether or not the


queue contains any elements. This operation is always
applicable.

8
Data Structures & Algorithms
Algorithm for Insert an Element in Queue
Algorithm insertq

Procedure insertq [ Q, F, R, N, Y]
// F and R pointers to the front and rear elements of a queue Q
// Consisting of N elements, and an element Y,
// this procedure insert Y at the rear of the queue.

1. [Overflow?]
if R >= N then
write( “overflow” )
return
2. [ Increment rear pointer]
R =R+1
3. [ Insert element]
Q[R] = Y
4. [Is Front Pointer properly set]
IF F=0 Then
F=1
return
9
Data Structures & Algorithms
Algorithm for Delete an Element in Queue
Algorithm removeq
Procedure removeq [ Q, F, R]
// F and R pointers to the front and rear elements of a queue Q
// to which they correspond, this function deletes and
// returns the last element of the queue
// Y is the temporary variable.

1. [Underflow ?]
if F= 0 then
write( “Underflow” )
return(0) [0 denotes an empty queue]
2. [ Delete an element ]
Y =Q[F]
3. [ Queue empty ?]
if F=R then
F=R=0
else
F=F+1 [Increment front pointer]
4. [Return element]
10
Return (Y)
Data Structures & Algorithms

Algorithm for Empty Queue

Algorithm empty (q)

{
If empty (&q)
{ write (“Queue is empty”);
return ture;
[queue is empty]
}
else
{ write (“Queue is not empty”);
return false;
[queue is not empty]
}
}

11

You might also like