1 queues and priority queues chapter 8. 2 introduction to queues a queue is a waiting line – seen...
Post on 21-Dec-2015
220 views
TRANSCRIPT
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
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
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
5
• 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
Designing and Building a Queue ClassArray-Based
6
• 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
Designing and Building a Queue ClassArray-Based
7
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–myCapacity
determinedat run time
8
Linked Queues
• Even with dynamic allocation of queue size– Difficult 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)
9
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
10
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
11
Application of Queues: Buffers and Scheduling
• Also times when insertions, deletions must be made from both ends– Consider a scrolling
window on the screen
• This requires a doubleended queue– Called a deque (pronounced "deck")
– Could also be considered a double ended stack (or "dack")
12
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
13
Application of Queues: Buffers and Scheduling
• Queues used toschedule taskswithin an operating system
• Job moves from disk to ready queue
14
Application of Queues: Buffers and Scheduling
• Ready queue may actuallybe a priority queue … job may get to "cut the line" based on its priority
15
Deque“double-ended queue”
• Push and pop from either side• Deques have the following properties:
– Individual elements can be accessed by their position index.
– Iteration over the elements can be performed in any order.
– Elements can be efficiently added and removed from any of its ends (either the beginning or the end of the sequence).
16
Deque• What methods are available?
Iterators:begin Return iterator to beginning end Return iterator to end rbegin Return reverse iterator to reverse beginning rend Return reverse iterator to reverse end
Capacity:size Return size max_size Return maximum size resize Change size (public member functions)empty Test whether container is empty
Element access:operator[] Access element at Access element front Access first element back Access last element
Modifiers:assign Assign container content push_back Add element at the end push_front Insert element at beginning pop_back Delete last element pop_front Delete first element insert Insert elements erase Erase elements swap Swap content clear Clear content
17
Deque#include <iostream>#include <deque>using namespace std;int main (){ unsigned int i; deque<int> first; deque<int> second (4,100); deque<int> third (second.begin(),second.end()); deque<int> fourth (third);
for (deque<int>::iterator it = third.begin();it!=third.end();it++) cout << *it << " "; cout << endl; int myints[] = {16,2,77,29}; deque<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); cout << "The contents of fifth are:"; for (i=0; i < fifth.size(); i++) cout << " " << fifth[i]; cout << endl; return 0;}
18
Deque
• How does it work internally?• Divided in several chunks of
storage• Deque class keeps all this
information and provides a uniform access to the elements.
• Deques are a little more complex internally.