queues - QUEUES A queue is a list from which items may be...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
QUEUES A queue is a list from which items may be deleted at one end (front) and into which items may be inserted at the other end (rear) Similar to checkout line in a grocery store - first come first served. It is referred to as a first-in-first-out (FIFO) data structure. Queues have many applications Simulation of real world problems - customer line management in banks - checkout points in supermarkets - airline flight scheduling - flight handling at airports……. . 1 front rear
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
in computer systems: - jobs in a single processor computer - print spooling - information packets in computer networks. - Primitive operations enqueue (q, x): inserts item x at the rear of the queue q x = dequeue (q): removes the front element from q and returns its value. isEmpty(q) : true if the queue is empty, otherwise false. Example enqueue(q, ‘A’); enqueue(q, ‘B’); enqueue(q, ‘C’); 2
Background image of page 2
x = dequeue(q); enqueue(q, ‘D’); enqueue(q, ‘E’); x= dequeue (q) -> x= ‘A’ Array Implementation A huge array and two variables (indices) front and rear to point the first and the last elements of the queue. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3 front rear A B C front rear B C D E
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5 3 8 1 1 9 4 Initially: q.rear = -1; q.front = 0; /* queue is empty when rear < front */ Addition and deletion are simple. Good if the queue is often emptied. Disadvantage: needs a huge array as the number of slots would go on increasing as long as there are items to be added to the list (irrespective of how many items are deleted, as these two are independent operations.) Ignoring overflow and underflow, insert and remove can be implemented as: 4 front rear
Background image of page 4
*/ enqueue(q, x): q.rear = q.rear +1; q.items[q.rear] = x; x = dequeue(q): x = q.items[q.front]; q.front = q.front + 1; Problems with this representation: Although there is space we may not be able to add a new item. An attempt will cause an overflow. 0 1
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 17

queues - QUEUES A queue is a list from which items may be...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online