Скачать презентацию Queues and Priority Queues Chapter 8 1 Скачать презентацию Queues and Priority Queues Chapter 8 1

f67cc583553d3f5613ac5353d0ff042b.ppt

  • Количество слайдов: 28

Queues and Priority Queues Chapter 8 1 Queues and Priority Queues Chapter 8 1

Introduction to Queues • A queue is a waiting line – seen in daily 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 2

Queue Basics • A queue is a sequence of data elements • In the 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) 3

Designing and Building a Queue Class Array-Based • Consider an array in which to Designing and Building a Queue Class Array-Based • Consider an array in which to store a queue • Note additional variables needed – my. Front, my. Back • Picture a queue object like this 4

Designing and Building a Queue Class Array-Based • Problems – We quickly Designing and Building a Queue Class Array-Based • 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 my. Back == my. Front 5

Designing and Building a Queue Class Array-Based • Using a static array – QUEUE_CAPACITY Designing and Building a Queue Class Array-Based • Using a static array – QUEUE_CAPACITY specified – Enqueue increments my. Back using mod operator, checks for full queue – Dequeue increments my. Front using mod operator, checks for empty queue 6

Using Dynamic Array to Store Queue Elements • Similar problems as with list and 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 – my. Capacity determined at run time 7

Linked Queues • Even with dynamic allocation of queue size – Difficult to adjust 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 (my. Capacity) 8

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

Application of Queues: Buffers and Scheduling • Important use of queues is I/O scheduling 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 10

Application of Queues: Buffers and Scheduling • Also times when insertions, deletions must be 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") 11

Application of Queues: Buffers and Scheduling • Consider a keyboard buffer – Acts as 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 12

Application of Queues: Buffers and Scheduling • Queues used to schedule tasks within an Application of Queues: Buffers and Scheduling • Queues used to schedule tasks within an operating system • Job moves from disk to ready queue 13

Application of Queues: Buffers and Scheduling • Ready queue may actually be a priority 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 14

Deque “double-ended queue” • Push and pop from either side • Deques have the 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). 15

Deque • What methods are available? Iterators: begin end rbegin rend Return iterator to Deque • What methods are available? Iterators: begin end rbegin rend Return iterator to beginning Return iterator to end Return reverse iterator to reverse beginning Return reverse iterator to reverse end Capacity: size max_size resize empty Return size Return maximum size Change size (public member functions) Test whether container is empty Element access: operator[] Access element at Access element front Access first element back Access last element Modifiers: assign push_back push_front pop_back pop_front insert erase swap clear Assign container content Add element at the end Insert element at beginning Delete last element Delete first element Insert elements Erase elements Swap content Clear content 16

Deque #include <iostream> #include <deque> using namespace std; int main () { unsigned int Deque #include #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; } 17

Deque • How does it work internally? • Divided in several chunks of storage 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. 18

19 19

20 20

21 21

22 22

23 23

24 24

25 25

26 26

27 27

28 28