180 likes | 199 Views
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)
E N D
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) • Game programming queue (Networking Game Programming) • Communication queues – between threads • Network queues - routers
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)
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&)
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?
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
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
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
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
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)
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
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?
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
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
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?
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
Application of Queues: Buffers and Scheduling • Queues used toschedule taskswithin an operating system • Job moves from disk to ready queue
Application of Queues: Buffers and Scheduling • Ready queue may actuallybe a priority queue … job may get to "cut the line" based on its priority