1 / 28

Queues

Queues. Linear list. One end is called front . Other end is called rear . Additions are done at the rear only. Removals are made from the front only. Bus Stop. Bus Stop Queue. front. rear. rear. rear. rear. rear. Bus Stop. Bus Stop Queue. front. rear. rear. rear.

agatha
Télécharger la présentation

Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queues • Linear list. • One end is called front. • Other end is called rear. • Additions are done at the rear only. • Removals are made from the front only.

  2. Bus Stop Bus Stop Queue front rear rear rear rear rear

  3. Bus Stop Bus Stop Queue front rear rear rear

  4. Bus Stop Bus Stop Queue front rear rear

  5. Bus Stop Bus Stop Queue front rear rear

  6. Queue Operations • IsEmpty … return true iff queue is empty • Push(insert) … add an element at the rear of the queue • Pop(delete) … delete the front element of the queue

  7. Queue in an Array • Use a 1D array to represent a queue. • Suppose queue elements are stored with the front element in queue[0], the next in queue[1], and so on.

  8. a b c d e 0 1 2 3 4 5 6 Derive From arrayList • Pop() => delete queue[0] • O(queue size) time • Push(x) => if there is capacity, add at right end • O(1) time

  9. O(1) Pop and Push • to perform each opertion in O(1) time (excluding array doubling), we use a circular representation.

  10. queue[] [2] [3] [1] [4] [0] [5] Custom Array Queue • Use a 1D array queue. • Circular view of array.

  11. [2] [3] A B C [1] [4] [0] [5] Custom Array Queue • Possible configuration with 3 elements.

  12. [2] [3] C [1] [4] B A [0] [5] Custom Array Queue • Another possible configuration with 3 elements.

  13. [2] [3] [2] [3] A B rear rear front front C C [1] [4] [1] [4] B A [0] [5] [0] [5] Custom Array Queue • Use integer variables front and rear. • front is one position counterclockwise from first element • rear gives position of last element

  14. [2] [3] A B rear front C [1] [4] [0] [5] Push An Element • Move rear one clockwise.

  15. [2] [3] A B front C [1] [4] [0] [5] rear Push An Element • Move rear one clockwise. • Then put into queue[rear]. D

  16. [2] [3] A B rear front C [1] [4] [0] [5] Pop An Element • Move front one clockwise.

  17. [2] [3] front A B rear C [1] [4] [0] [5] Pop An Element • Move front one clockwise. • Then extract from queue[front].

  18. [2] [3] A B rear front C [1] [4] [0] [5] Moving rear Clockwise • rear++; • if(rear = = capacity) rear = 0; • rear = (rear + 1) % capacity;

  19. [2] [3] rear front C [1] [4] B A [0] [5] Empty That Queue

  20. [2] [3] rear C [1] [4] B [0] [5] Empty That Queue front

  21. [2] [3] rear C [1] [4] [0] [5] Empty That Queue front

  22. [2] [3] rear [1] [4] [0] [5] Empty That Queue • When a series of removes causes the queue to become empty, front = rear. • When a queue is constructed, it is empty. • So initialize front = rear = 0. front

  23. [2] [3] rear front C [1] [4] B A [0] [5] A Full Tank Please

  24. [2] [3] rear front [1] [4] [0] [5] A Full Tank Please D C B A

  25. [2] [3] front [1] [4] [0] [5] A Full Tank Please rear D E C B A

  26. [2] [3] front [1] [4] [0] [5] A Full Tank Please D E C F B A rear • When a series of adds causes the queue to become full, front = rear. • So we cannot distinguish between a full queue and an empty queue!

  27. Ouch!!!!! • Remedies. • Don’t let the queue get full. • When the addition of an element will cause the queue to be full, increase array size. • This is what the text does.

  28. Ouch!!!!! • Remedies (continued). • Define an integer variable size. • Following each push do size++. • Following each pop do size--. • Queue is empty iff (size == 0) • Queue is full iff (size == arrayLength)

More Related