1 / 18

Queues

Queues. CPS212 Gordon College. Introduction to Queues. A queue is a waiting line – seen in daily life Real world examples – toll booths, bank, food Plenty of CS examples: Printer queue, server queues Event queue for programs (controls program interaction)

hensleyr
Download Presentation

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 CPS212 Gordon College

  2. Introduction to Queues • A queue is a waiting line – seen in daily life • Real world examples – toll booths, bank, food • Plenty of CS examples: • Printer queue, server queues • Event queue for programs (controls program interaction) • Game programming queue (Networking Game Programming) • Communication queues – between threads • Network queues - routers

  3. Queue Basics • A queue is a sequence of data elements • In the sequence • Items can be removed only at the front • Items can be added only at the other end, the back • 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)

  4. STL Queue value_type size_type queue() queue(const queue&) The copy constructor. queue& operator=(const queue&) The assignment operator. bool empty() const size_type size() const value_type& front() const value_type& front() const value_type& back() void push(const value_type&) void pop() bool operator==(const queue&, const queue&) bool operator<(const queue&, const queue&)

  5. STL Queue Implementation Design criteria: • Elements accessed only from front • Elements efficiently added to back • Elements efficiently removed from front • No guarantee to contiguous elements What sort of designs would work? What is the better design and why?

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

  7. Designing and Building a Queue ClassArray-Based • Problems • We quickly "walk off the end" of the array • Possible solutions • Shift array elements • Use a circular queue • Ring buffer • Note that both emptyand full queuegives myBack == myFront

  8. Designing and Building a Queue ClassArray-Based • 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

  9. Using Dynamic Array to Store Queue Elements • Similar problems as with list and stack • Fixed size array can be specified too large or too small • Dynamic array design allows sizing of array for multiple situations • Results in structure as shown • myCapacitydeterminedat run time

  10. Linked Queues • Even with dynamic allocation of queue size • Difficult/Inefficient to adjust during run of program • Could use linked list to store queue elements • Can grow and shrink to fit the situation • No need for upper bound (myCapacity)

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

  12. STL Queues • implemented as containers adaptors (like stack) • classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access it elements. • underlying container may be one of the standard container class template or some other specifically designed container class. • must support the following operations: • front() • back() • push_back() • pop_front() Which of the container classes will work?

  13. STL Queues List Deque list<int> fl(5,100); queue<int, list<int> > f(fl); while (!f.empty()) { cout << f.front() << " "; f.pop(); } cout << endl; deque<int> fl(5,100); queue<int, deque<int> > f(fl); while (!f.empty()) { cout << f.front() << " "; f.pop(); } cout << endl; CBuffer<int> t(5,100); queue<int, CBuffer<int> > f5(t); while (!f5.empty()) { cout << f5.front() << "**"; f5.pop(); } cout << endl; Circular Buffer Make you own See circular_buffer example

  14. Application of Queues: Buffers and Scheduling • Important use of queues is I/O scheduling • Use buffers in memory to improve program execution • Buffer arrangedin FIFO structure

  15. Application of Queues: Buffers and Scheduling • Also times when insertions, deletions must be made from both ends • Consider a scrollingwindow on the screen • This requires a doubleended queue • Would a deque serve the purpose?

  16. Application of Queues: Buffers and Scheduling • Consider a keyboard buffer • Acts as a queue • But elements may be removed from the back of the queue with backspace key • A printer spool is a queue of print jobs

  17. Application of Queues: Buffers and Scheduling • Queues used toschedule taskswithin an operating system • Job moves from disk to ready queue

  18. Application of Queues: Buffers and Scheduling • Ready queue may actuallybe a priority queue … job may get to "cut the line" based on its priority

More Related