{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

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

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

View Full Document
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
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

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

View Full Document
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
/* number of elements in the queue = rear – front + 1 */ 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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}