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.

Slides:



Advertisements
Similar presentations
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Advertisements

Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Queues and Priority Queues Chapter 8. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank,
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Chapter 16 Stack and Queues part2
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Exam Review 5/28/15. Software Development (Review)  Make it work…then make it pretty can result in dangerous code  Better to take a unified, consistent.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Data Structures and Algorithm Analysis Dr. Ken Cosh Stacks ‘n’ Queues.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Introduction Of Queue. Introduction A queue is a non-primitive linear data structure. It is an homogeneous collection of elements in which new elements.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
1 Data Organization Example 1: A simple text editor –Store the text buffer as a list of lines. –How would we implement the UNDO operation? Example 2: Parsing.
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
Data Structures Using C, 2e
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
Queues.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Queues.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Algorithms and Data Structures
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Chapter 19: Stacks and Queues.
Queues.
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
[Most of the details about queues are left for to read about and work out in Lab 6.] Def. As a data structure, a queue is an ordered collection of data.
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

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

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