Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Queue

2,078 views

Published on

Its Presentation of Queue(Programming).
May it will help u :)

  • Be the first to comment

Queue

  1. 1. Introduction To Algorithm
  2. 2. Abstract Data Type Stack & Queue is an example of ADT An array is not ADT.
  3. 3. Using a Queue Several example applications of queues are given in that Slides. This presentation describes the queue operations and two ways to implement a queue. Data Structures and Other Objects Using C++
  4. 4. What is Queue? A QUEUE IS A CONTAINER IN WHICH INSERTIONS ARE MADE ONLY AT THE BACK DELETIONS, RETRIEVALS, AND MODIFICATIONS ARE MADE ONLY AT THE FRONT.
  5. 5. The Queue Operations A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ Front Rear
  6. 6. EnQueue Operations New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. $ $ Front Rear
  7. 7. DeQueue Operations  When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ $ Front Rear
  8. 8. Array Implementation A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6 AAnn aarrrraayy ooff iinntteeggeerrss ttoo iimmpplleemmeenntt aa qquueeuuee ooff iinntteeggeerrss WWee ddoonn''tt ccaarree wwhhaatt''ss iinn tthhiiss ppaarrtt ooff tthhee aarrrraayy..
  9. 9. Array Implementation The efficient implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6 3 ssiizzee 0 ffiirrsstt 2 llaasstt
  10. 10. A Dequeue Operation When an element leaves the queue, size is decremented, and first changes, too. 2 ssiizzee 1 ffiirrsstt 2 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6
  11. 11. An Enqueue Operation When an element enters the queue, size is incremented, and last changes, too. 3 ssiizzee 1 ffiirrsstt 3 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 8 6 2
  12. 12. At the End of the Array There is special behaviour at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: 2 3 ssiizzee 3 ffiirrsstt 5 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] 5 6 1
  13. 13. At the End of the Array The new element goes at the front of the array (if that spot isn’t already used): 4 ssiizzee 3 ffiirrsstt 0 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] 4 2 6 1
  14. 14. Array Queue Reviews  Easy to implement.  But it has a limited capacity with a fixed array  Special behaviour is needed when the rear reaches the end of the array. [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6 3 ssiizzee 0 ffiirrsstt 2 llaasstt
  15. 15. Linked List Implementation A queue can also be implemented with a linked list with both a head and a tail pointer. null 10 15 7 13 Head-ptr Tail-ptr
  16. 16. Linked List Implementation Which end do you think is the front of the queue? Why ? 10 15 7 13 Head-ptr Tail-ptr null
  17. 17. Linked List Implementation The head-ptr points to the front of the list. Because it is harder to remove items from the tail of the list. Rear Front 10 15 7 13 Head-ptr Tail-ptr null
  18. 18. The Queue Class template <class Item> class queue<Item> { public: queue( ); void push(const Item& entry); void pop( ); bool empty( ) const; Item front( ) const; }  The C++ standard template library has a queue template class.  The template parameter is the type of the items that can be put in the queue.
  19. 19. Summary QQuueeuueess hhaavvee mmaannyy aapppplliiccaattiioonnss..  IItteemmss eenntteerr aa qquueeuuee aatt tthhee rreeaarr aanndd lleeaavvee aa qquueeuuee aatt tthhee ffrroonntt.. QQuueeuueess ccaann bbee iimmpplleemmeenntteedd uussiinngg aann aarrrraayy oorr uussiinngg aa lliinnkkeedd lliisstt..
  20. 20. Daily Life Examples There's a queue of questions that you've asked in exam, all waiting for you to accept answers for them. Waiting in a queue in any bank for paying bills, depositing cash OR cheques.
  21. 21. Thank You THE END

×