Queues

7,931 views

Published on

Queues(Data structure)

2 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total views
7,931
On SlideShare
0
From Embeds
0
Number of Embeds
441
Actions
Shares
0
Downloads
443
Comments
2
Likes
3
Embeds 0
No embeds

No notes for slide

Queues

  1. 1. Data Structures BIM , 3 nd Semester Tribhuvan University
  2. 2. Lecture 1: Queues Readings: Chapter 4
  3. 3. What is a Queue <ul><li>In ordinary English a queue is defined as a waiting line, like a line of people waiting to purchase tickets </li></ul><ul><ul><li>Here, the first person in the line is the first person served </li></ul></ul><ul><li>A queue is an ordered collection of items in which all insertions are made at one end (the rear ), and all deletions are made at the other end (the front ) </li></ul>
  4. 4. Queues 12 69 50 32 78 88 front rear front rear A waiting queue Computer Implementation of queue
  5. 5. Enqueue & Dequeue Operations <ul><li>Enqueue – Inserts an item at the rear of the queue. Other names – Add , Insert </li></ul><ul><li>Dequeue – Deletes an item from the front of the queue. Other names – Delete , Remove , Serve </li></ul><ul><li>Since it is always the first item to be put into the queue that is the first item to be removed, a queue is a first-in , first-out or FIFO list </li></ul><ul><li>Front and rear are also called head and tail respectively </li></ul>
  6. 6. Illustration 78 69 50 32 69 50 32 Enqueue(78) 78 69 50 32 Enqueue(88) 88 78 50 32 88 Dequeue() -> 69 78 32 88 Dequeue() -> 50 f r r f f r f r f r A queue is a FIFO list
  7. 7. Applications <ul><li>Direct Applications </li></ul><ul><ul><li>Access to shared resources </li></ul></ul><ul><ul><ul><li>Within a computer system there may be queues of tasks waiting for the printer, for access to disk storage, or even in a time-sharing system, for use of the CPU </li></ul></ul></ul><ul><li>Indirect Applications </li></ul><ul><ul><li>Auxiliary data structure for algorithms </li></ul></ul><ul><ul><li>Component of other data structures </li></ul></ul>
  8. 8. Checking Palindromes <ul><li>Read each letter in the phrase. Enqueue the letter into the queue, and push the letter onto the stack. </li></ul><ul><li>After we have read all of the letters in the phrase: </li></ul><ul><ul><li>Until the stack is empty, Dequeue a letter from the queue and pop a letter from the stack. </li></ul></ul><ul><ul><li>If the letters are not the same, the phrase is not a palindrome </li></ul></ul>
  9. 9. The Queue ADT <ul><li>A queue of elements of type T is a finite sequence of elements of T together with the operations </li></ul><ul><ul><li>MakeEmpty( Q ) – Create an empty queue Q </li></ul></ul><ul><ul><li>Empty( Q ) – Determine if the queue Q is empty or not </li></ul></ul><ul><ul><li>Enqueue( Q , x ) – Insert element x at the end of the queue Q </li></ul></ul><ul><ul><li>Dequeue( Q ) – If the queue Q is not empty, remove the element at the front of the queue </li></ul></ul><ul><ul><li>Front( Q ) – Retrieve the element at the front of the queue Q , without deleting it </li></ul></ul>
  10. 10. Implementation of Queues <ul><li>The physical model: a linear array with the front always in the first position and all entries moved up the array whenever the front is deleted </li></ul><ul><li>Array Implementation: </li></ul><ul><ul><li>A linear array with two indices always increasing </li></ul></ul><ul><ul><li>A circular array with front and rear indices and one position left vacant </li></ul></ul><ul><li>Linked List </li></ul><ul><ul><li>A linked list with pointers to the front and rear nodes </li></ul></ul>
  11. 11. Linear Implementation <ul><li>For all type of array implementation, we set up an array to hold the items in the queue </li></ul><ul><li>In linear implementation, we use two indices to keep track the front and the rear of the queue </li></ul><ul><li>To enqueue an item , we increase the rear by one and put the item in that position </li></ul><ul><li>To dequeue an item , we take it from the position at the front and then increase front by one </li></ul>
  12. 12. The QueueType Declaration #define MAXQUEUESIZE 100 typedef int ItemType; typedef struct { ItemType items[MAXQUEUESIZE]; int front, rear; } QueueType;
  13. 13. The MakeEmpty Operation <ul><li>To initialize an empty queue, we set front to 0 and rear to -1 </li></ul>void MakeEmpty(QueueType *pq) { pq->front = 0; pq->rear = -1; }
  14. 14. Illustration Enqueue(32) Enqueue(18) Enqueue(58) Dequeue() -> 32 Dequeue() -> 18 Enqueue(68) Enqueue(96) A queue with an array of size 5 r = -1, f = 0 1 0 2 3 4 r = 2 18 32 58 1 0 2 3 4 f = 0 f = 2, r = 2 58 1 0 2 3 4 f = 2 58 68 96 1 0 2 3 4 r = 4
  15. 15. The Full and Empty function int Empty(QueueType *pq) { return pq->rear < pq->front; } int Full(QueueType *pq) { return pq->rear == MAXQUEUESIZE-1; }
  16. 16. The Enqueue function void Enqueue(QueueType *pq, ItemType newitem) { if (Full(pq)) { printf(&quot;Queue Full &quot;,); exit(1); } pq->items[++pq->rear] = newitem; }
  17. 17. The Dequeue function ItemType Dequeue(QueueType *pq) { if (Empty(pq)) { printf(&quot;Queue is Empty &quot;,); exit(1); } return pq->items[pq->front++]; }
  18. 18. Problems with Linear Implementation <ul><li>Both the rear and front indices are increased but never decreased </li></ul><ul><li>As items are removed from the queue, the storage space at the beginning of the array is discarded and never used again </li></ul>This queue is considered full, even though the space at beginning is vacant f = 2 58 68 96 1 0 2 3 4 r = 4
  19. 19. Circular Array Implementation <ul><li>View the array, holding the queue, as a circle rather than a straight line </li></ul><ul><li>Imagine the first element of the array (i.e., the element at index 0) as immediately following its last element </li></ul><ul><li>If incrementing either rear or front causes it to go past the array, reset its value to zero </li></ul><ul><li>With this approach, we can always insert an item unless the array is fully occupied </li></ul>
  20. 20. Illustration Enqueue(21) Dequeue() -> 58 Dequeue() -> 68 Enqueue(81) Dequeue() -> 96 f = 2 58 68 96 1 0 2 3 4 r = 4 f = 2 21 58 68 96 1 0 2 3 4 r = 0 f = 4 21 96 1 0 2 3 4 r = 0 f = 4 81 21 96 1 0 2 3 4 r = 1 f = 0 81 21 1 0 2 3 4 r = 1
  21. 21. Checking Boundary Conditions <ul><li>The condition rear < front does not hold for empty queue </li></ul>In all cases, rear < front but the queue is not empty f = 2 21 58 68 96 1 0 2 3 4 r = 0 f = 4 21 96 1 0 2 3 4 r = 0 f = 4 81 21 96 1 0 2 3 4 r = 1
  22. 22. Checking Boundary Conditions <ul><li>Same relative positions for an empty and a full queue </li></ul>Dequeue() -> 58 Enqueue(97) Empty queue Full queue r = 2, f = 2 58 1 0 2 3 4 f = 3 58 21 68 96 1 0 2 3 4 r = 1 f = 3 1 0 2 3 4 r = 2 f = 3 58 21 97 68 96 1 0 2 3 4 r = 2
  23. 23. Possible Solutions <ul><li>At least 3 ways to resolve the problem </li></ul><ul><ul><li>Leave one empty position in the array so that the queue is considered full when the rear index has moved within two positions ahead of front </li></ul></ul><ul><ul><ul><li>For convenience, initialize both rear and front to same value for empty queue </li></ul></ul></ul>We will use this method
  24. 24. Possible Solutions (Cont…) <ul><ul><li>Introduce a Boolean variable that will indicate whether the queue is full (or empty) or not, or an integer variable that counts the no of items in the queue </li></ul></ul><ul><ul><li>Set one or both of the indices to some values that would otherwise never occur in order to indicate an empty (or full) queue. </li></ul></ul><ul><ul><ul><li>For e.g., an empty queue could be indicated by setting the rear index to -1 </li></ul></ul></ul>
  25. 25. The MakeEmpty and Empty Function <ul><li>For circular queue, we set both front and rear to MAXQUEUESIZE-1 </li></ul>void MakeEmpty(QueueType *pq) { pq->front = MAXQUEUESIZE-1; pq->rear = MAXQUEUESIZE-1; } int Empty(QueueType *pq) { return pq->rear == pq->front; }
  26. 26. Why to sacrifice one element? Enqueue(35) Enqueue(21) rear == front, but queue is not empty, so sacrifice one element f = 1 58 68 96 1 0 2 3 4 r = 4 r = 0 f = 1 21 58 68 96 1 0 2 3 4 r = 1, f = 1 35 21 58 68 96 1 0 2 3 4
  27. 27. The Full Function <ul><li>The queue is full, if rear is just one position ahead of front </li></ul>int Full(QueueType *pq) { int newrear; newrear = (pq->rear+1)%MAXQUEUESIZE; return newrear == pq->front; }
  28. 28. The Enqueue Function void Enqueue(QueueType *pq, ItemType newitem) { if (Full(pq)) { printf(&quot;Queue Full &quot;,); exit(1); } pq->rear = (pq->rear+1)%MAXQUEUESIZE; pq->items[pq->rear] = newitem; }
  29. 29. The Dequeue Function ItemType Dequeue(QueueType *pq) { if (Empty(pq)) { printf(&quot;Queue is Empty &quot;,); exit(1); } pq->front = (pq->front+1)%MAXQUEUESIZE; return pq->items[pq->front]; }
  30. 30. Summary of Array Implementation of Queues <ul><li>A linear array with the front always in the first position and all entries moved up the array whenever the front is deleted </li></ul><ul><li>A linear array with two indices always increasing </li></ul><ul><li>A circular array with front and rear indices and one position left vacant </li></ul><ul><li>A circular array with front and rear indices and either a Boolean variable to indicate fullness (or emptiness) or an integer variable counting entries </li></ul><ul><li>A circular array with front and rear indices taking special values to indicate emptiness </li></ul>
  31. 31. Priority Queues <ul><li>In stacks and queues, the elements are ordered are based on the sequence in which they have been inserted </li></ul><ul><li>A priority queue is a data structure in which the intrinsic ordering of the elements determines the results of its basic operations </li></ul>
  32. 32. Types of Priority Queues <ul><li>Ascending (Min) Priority Queue </li></ul><ul><ul><li>A collection of items into which items can be inserted arbitrarily but only the smallest item can be removed </li></ul></ul><ul><li>Descending (Max) Priority Queue </li></ul><ul><ul><li>A collection of items into which items can be inserted arbitrarily but only the largest item can be removed </li></ul></ul>
  33. 33. Illustration 69 50 32 50 32 78 Insert(78) 78 69 50 32 Insert(88) 88 88 69 50 78 DeleteMin() -> 32 88 69 78 DeleteMin() -> 50 A min priority queue 69
  34. 34. Applications of Priority Queue <ul><li>In a time-sharing computer system, a large number of tasks may be waiting for the CPU </li></ul><ul><li>Some of these tasks have higher priority than others </li></ul><ul><li>The set of tasks waiting for the CPU forms a priority queue </li></ul>
  35. 35. The Priority Queue ADT <ul><li>A ( min ) priority queue of elements of type T is a finite sequence of elements of T together with the operations </li></ul><ul><ul><li>MakeEmpty( P ) – Create an empty priority queue P </li></ul></ul><ul><ul><li>Empty( P ) – Determine if the priority queue P is empty or not </li></ul></ul><ul><ul><li>Insert( P , x ) – Add element x on the priority queue P </li></ul></ul><ul><ul><li>DeleteMin( P ) – If the priority queue P is not empty, remove the minimum element of the queue and return it </li></ul></ul><ul><ul><li>FindMin( P ) – Retrieve the minimum element of the priority queue P </li></ul></ul>
  36. 36. Array Implementation of Priority Queue <ul><li>Unordered Array Implementation </li></ul><ul><ul><li>To insert an item, insert it at the rear end of the queue </li></ul></ul><ul><ul><li>To delete an item, find the position of the minimum element and </li></ul></ul><ul><ul><ul><li>either mark it as deleted (lazy deletion) or </li></ul></ul></ul><ul><ul><ul><li>shift all elements past the deleted element by one position and then decrement rear </li></ul></ul></ul>
  37. 37. Illustration 69 50 32 78 69 50 32 Insert(88) 88 50 78 69 88 DeleteMin() -> 32 r f f r f r 78 -1 78 69 -1 -1 DeleteMi() -> 50 88 f r 50 78 69 88 f r -1 The value -1 marks these entries as deleted
  38. 38. Array Implementation of Priority Queue <ul><li>Ordered Array Implementation </li></ul><ul><ul><li>Maintain the queue as a circular ordered array making the front as the position of the smallest element and the rear as the position of the largest element </li></ul></ul><ul><ul><li>To insert an element, locate the proper position of the new element and shift preceding or succeeding elements by one position </li></ul></ul><ul><ul><li>To delete the minimum element, increment the front position </li></ul></ul>
  39. 39. Illustration 50 69 78 32 46 50 69 Insert(46) 78 50 46 78 DeleteMin() -> 32 r f f r f r 32 69 50 69 78 46 50 69 72 Insert(72) 78 69 50 78 DeleteMin() -> 46 r f f r f r 46 72
  40. 40. Notes on Array Implementation <ul><li>The array implementation of priority queue is not satisfactory </li></ul><ul><li>Either the Insert operation or DeleteMin operation is proportional to n ( n is the queue size), i.e., O ( n ) </li></ul><ul><li>A heap allows to implement the Insert and DeleteMin in O (log n ) time </li></ul>
  41. 41. Deque <ul><li>A deque ( double-ended queue ) is a data structure consisting of a list of items, on which the following operations are possible </li></ul><ul><ul><li>Push( D , x ) – Insert item x on the front of deque D </li></ul></ul><ul><ul><li>Pop( D ) – Remove the front item from deque D and return it </li></ul></ul><ul><ul><li>Inject( D , x ) – Insert item x on the rear end of deque D </li></ul></ul><ul><ul><li>Eject( D ) – Remove the rear item from deque D and return it </li></ul></ul>
  42. 42. Illustration 69 50 32 69 50 32 Push(78) 78 69 50 32 Inject(88) 88 32 69 50 88 Pop() -> 78 50 69 32 Eject() -> 88 f r r f f r f r f r A deque 78
  43. 43. The End

×