1 / 30

Chapter 16 Stack and Queues part2

Chapter 16 Stack and Queues part2. Dr. Bernard Chen Ph.D. University of Central Arkansas. Introduction to Queues. A queue is a waiting line It’s in daily life: A line of persons waiting to check out at a supermarket A line of persons waiting to purchase a ticket for a film

walker-clay
Download Presentation

Chapter 16 Stack and Queues part2

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. Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas

  2. Introduction to Queues • A queue is a waiting line • It’s in daily life: • A line of persons waiting to check out at a supermarket • A line of persons waiting to purchase a ticket for a film • A line of planes waiting to take off at an airport • A line of vehicles at a toll booth

  3. Introduction to Queues • Difference between Stack and Queues: • Stack exhibits last-in-first-out (LIFO) • Queue exhibits first-in-first-out (FIFO)

  4. ADT in Queues • Unlike stacks in which elements are popped and pushed only at the ends of the list, Collection of data elements: • items are removed from a queue at one end, called the FRONT of the queue; • and elements are added at the other end, called the BACK

  5. Queue ADT • Basic operations • Construct a queue • Check if empty • Enqueue (add element to back) • Front (retrieve value of element from front) • Dequeue (remove element from front)

  6. Designing and Building a Queue Class Array-Based • Consider an array in which to store a queue • Note additional variables needed • myFront, myBack • Picture a queueobject like this

  7. Empty Queue Enqueue(70) Queue Operation

  8. Queue Operation • Enqueue(80) • Enqueue(50)

  9. Queue Operation • Dequeue() • Dequeue()

  10. Queue Operation • Enqueue(90) • Enqueue(60)

  11. Circular Queue • Problems • We quickly "walk off the end" of the array • Possible solutions • Shift array elements • Use a circular queue • Note that both emptyand full queuegives myBack == myFront

  12. Circular Queue • Using a static array • QUEUE_CAPACITY specified • Enqueue increments myBack using mod operator, checks for full queue • Dequeue increments myFront using mod operator, checks for empty queue

  13. Circular Example Both Front and Back wraparound as needed. b c d e f Back Front g b c d e f Back Front

  14. QUEUE • Only tricky part is vector doubling because the queue items are not necessarily stored in an array starting at location 0, and the contiguity of wraparound must be maintained. • Therefore, mostly straightforward; maintain • Front • Back

  15. Queue Full Situation • If an item were stored in the last position, and an Enqueure() occurred • myBack would be incremented by 1, giving it the same value as myFront • However, myFront == myBack indicates the queue is empty • Thus, we cannot distinguish between empty and full • We may avoid this situation by maintaining one empty position, so that myFront will never equal to myBack unless the queue is empty

  16. Queue Operation • Construct: Create an array, set capacity, myFront=myBack=0 • Empty: test myFront==myBack • Front : if not empty: print array[myFront]

  17. Algorithm for Enqueue(value) • 1. Set newBack == (myBack+1)%Queue_capacity • 2. If newBack == myFront Signal “Queue Full” otherwise: Set Array[myBack] == value Set myBack == newBack

  18. Algorithm for Dequeue() • If queue is empty signal “Queue Empty” Otherwise Set myFront=(myFront+1)%Queue_capacity

  19. Linked Queues • We could also use linked list to store queue elements • Can grow and shrink to fit the situation • No need for upper bound (myCapacity)

  20. Linked Queues • Constructor initializes myFront, myBack • Empty • myFront == Null • Front • return myFront->data • Dequeue • Delete first node (watch for empty queue) • Enqueue • Insert node at end of list

  21. Enqueue newptr= new Node(value) if (empty()) myFront=myBack=newptr; else { myBack->next=newptr; myBack=newwptr; }

  22. Dequeue (if not empty) ptr=myFront myFront=myFront->next delete ptr;

  23. Queue ADT implement by Vectors Basic operations Construct a queue Check if empty Enqueue (add element to back) Front (retrieve value of element from front) Dequeue (remove element from front)

  24. Enqueue and Front Enqueue (add element to back) This is the same with push(), therefore: L.push_back(value); Front (retrieve value of element from front) L.begin();

  25. Dequeue Dequeue (remove element from front) L.erase(L.begin()); L.begin() is an iterator, in erase(), you cannot just gives the location

  26. Functions related to Queue • Constructor: vector<int> L; • Empty(): L.size() == 0? • Enqueue(): L.push_back(value); • Front(): L.begin(); • Dequeue(): L.erase(L.begin());

  27. QUEUE Write a queue program

  28. Vector Functions

  29. Queue + Stack SCROLL: a queue-stack hybrid model Thus, you may have functions in both— Push(value)=Enqueue(value) Pop(); Top() --- top of stack Dequeue(); Front()

More Related