Download presentation
Presentation is loading. Please wait.
1
1 Queues and Priority Queues Chapter 8
2
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
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
4 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 queue object like this
5
5 Problems –We quickly "walk off the end" of the array Possible solutions –Shift array elements –Use a circular queue –Note that both empty and full queue gives myBack == myFront Designing and Building a Queue Class Array-Based
6
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 Class Array-Based
7
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 determined at run time
8
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
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
10 Application of Queues: Buffers and Scheduling Important use of queues is I/O scheduling –Use buffers in memory to improve program execution –Buffer arranged in FIFO structure
11
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 double ended queue –Called a deque (pronounced "deck") –Could also be considered a double ended stack (or "dack")
12
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
13 Application of Queues: Buffers and Scheduling Queues used to schedule tasks within an operating system Job moves from disk to ready queue
14
14 Application of Queues: Buffers and Scheduling Ready queue may actually be a priority queue … job may get to "cut the line" based on its priority
15
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
16 Deque What methods are available? Iterators: beginReturn iterator to beginning endReturn iterator to end rbeginReturn reverse iterator to reverse beginning rendReturn reverse iterator to reverse end Capacity: sizeReturn size max_sizeReturn maximum size resizeChange size (public member functions) emptyTest whether container is empty Element access: operator[]Access element atAccess element frontAccess first element backAccess last element Modifiers: assignAssign container content push_backAdd element at the end push_frontInsert element at beginning pop_backDelete last element pop_frontDelete first element insertInsert elements eraseErase elements swapSwap content clearClear content
17
17 Deque #include using namespace std; int main () { unsigned int i; deque first; deque second (4,100); deque third (second.begin(),second.end()); deque fourth (third); for (deque ::iterator it = third.begin();it!=third.end();it++) cout << *it << " "; cout << endl; int myints[] = {16,2,77,29}; deque 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
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.
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.