Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Queues CPS212 Gordon College

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 Network queues - routers

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 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 5 STL Queue Implementation Design criteria: 1.Elements accessed only from front 2.Elements efficiently added to back 3.Elements efficiently removed from front 4.No guarantee to contiguous elements What sort of designs would work? What is the better design and why?

6 6 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

7 7 Problems –We quickly "walk off the end" of the array Possible solutions –Shift array elements –Use a circular queue Ring buffer –Note that both empty and full queue gives myBack == myFront Designing and Building a Queue Class Array-Based

8 8 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

9 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 –myCapacity determined at run time

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

14 14 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

15 15 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 –Would a deque serve the purpose?

16 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 17 Application of Queues: Buffers and Scheduling Queues used to schedule tasks within an operating system Job moves from disk to ready queue

18 18 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


Download ppt "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."

Similar presentations


Ads by Google