Queue
Queue
QUEUE
• A queue is a linear list of elements in which
deletions can take place only at one end,
called the Front and insertions can take place
at the other end called the Rear.
• Its insertion and removal routines follows the
first-in-first-out (FIFO) principle.
• Queues may be represented by linear arrays
or one way lists
Queue Applications
• Real life examples
– Waiting in line
• Applications related to Computer Science
– Job scheduling (e.g. Round-Robin algorithm for
CPU allocation)
QUEUE ADT
• ADT queue is
– Object: a finite ordered list with zero or more
elements
– Functions:
• createQ(maxqueuesize)
– Creates an empty queue with size=maxqueuesize
• Isfull(queue,maxqueuesize)-
– If (number of elements in queue=maxqueuesize) return true
– Else return false
• Isempty(queue)
– if queue==createQ(maxqueueusize) return true
– Else return false
• AddQ(queue,item)
– If Isfullqueue(queue) return msg(“queue is full;
cannot add”)
– Else insert item to the rear of the queue and return
• DeleteQ(queue)
– If Isemptyqueue(queue) return msg (“Queue is
empty”)
– Else remove and return item from front of queue
Implementing a Queue with an array
• The queue will be maintained by a linear array
called QUEUE and the variable FRONT
contains the location of the front element of
the queue and the variable REAR contains the
location of the rear element of the queue.
• The condition that FRONT=0 or NULL will
indicate that the queue is empty.
• Whenever an element is deleted from the
queue front is incremented and whenever an
element is inserted rear is incremented.
Array representation of a QUEUE
AAA BBB CCC DDD
Front=1 , Rear=4
Front=1, Rear=7
AAA BBB CCC DDD EEE FFF GGG HHH
Inserting a new element, Front=1, Rear=8
No more insertions possible (Overflow)
Front
Rear
ww
xxx yyy .
w
Insert a node
Front Rear