Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

Similar documents
! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

ADTs: Stacks and Queues

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Chapter 18: Stacks And Queues

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 18: Stacks And Queues

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Queue with Array Implementation

An Introduction to Queues With Examples in C++

ITI Introduction to Computing II

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

SYSC 2006 Winter 2012 Linear Collections: Queues

Lecture Data Structure Stack

Queue: Queue Representation: Basic Operations:


CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Queues. Queue ADT Queue Implementation Priority Queues

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CSEN 301 Data Structures and Algorithms

CS24 Week 4 Lecture 2

Data Structure. Recitation VII

STACKS AND QUEUES. Problem Solving with Computers-II

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

CS302 - Data Structures using C++

UNIT-2 Stack & Queue

Stacks and Queues. CSE 373 Data Structures Lecture 6

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Stacks and Queues III

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

Ashish Gupta, Data JUET, Guna

ADTs Stack and Queue. Outline

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

CSC 1052 Algorithms & Data Structures II: Queues

LIFO : Last In First Out

Lecture 4 Stack and Queue

Priority Queue ADT. Revised based on textbook author s notes.

Stack and Queue. Stack:

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Stacks and Queues. CSE Data Structures April 12, 2002

ADTs Stack and Queue. Outline

CSC 1052 Algorithms & Data Structures II: Linked Queues

What Can You Use a Queue For?

Chapter 2. Stack & Queues. M hiwa ahmad aziz

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

1. Introduction. Lecture Content

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

More Data Structures (Part 1) Stacks

Stacks and Queues. Chapter 8 (Cont.)

1 P age DS & OOPS / UNIT II

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Programming Abstractions

Object Oriented Programming COP3330 / CGS5409

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

CMSC 341. Deques, Stacks and Queues 9/22/04 1

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Data Structures and Algorithms for Engineers

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

Announcements Queues. Queues

Data Abstraction and Specification of ADTs

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

CS302 - Data Structures using C++

Queue Definition. Ordered list with property:

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

Abstract Data Types. Abstract Data Types

Introduction to Data Structures and Algorithms

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Chapter 5. ADTs Stack and Queue

ITI Introduction to Computing II

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Name CPTR246 Spring '17 (100 total points) Exam 3

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

Queues Fall 2018 Margaret Reid-Miller

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

A queue is a linear collection whose elements are added on one end and removed from the other

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

CS 2604 Homework 1 C++ Review Summer I 2003

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

CS 106B Lecture 5: Stacks and Queues

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

Your Topic Goes Here Queue

Motivation for Queues

Data Structures (INE2011)

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

== isn t always equal?

CMSC 132: Object-Oriented Programming II. Stack and Queue

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Transcription:

Queues Gaddis 18.4, 18.6 Molly A. O'Neil CS 2308 :: Spring 2016

The Queue ADT A queue is an abstract data type that stores a collection of elements of the same type The elements of a queue are accessed in FIFO (first in, first out) order No random access to other elements Can only add element at back, can only access element at front Examples: People in line at the bank; print jobs sent to a shared printer 2

Queue Operations The queue ADT supports the following operations: enqueue: add a value onto the rear of the queue (at the end of the line) If it's a size-limited queue, first make sure queue isn't full dequeue: remove the value at the front of the queue ("Next!") Depending on the implementation, sometimes dequeue returns the value as well isempty: true if the queue currently contains no elements Optionally, a queue may also support the following operations: peek/front: returns the value at the front of the queue, without removing it isfull: true if the queue is currently full, i.e., has no more space to hold additional elements 3

Queue Illustrated q.enqueue(3) q.enqueue(2) q.enqueue(5) 3 2 5 1 q.enqueue(1) x = q.dequeue() 4

Queue Illustrated q.enqueue(3) q.enqueue(2) q.enqueue(5) 2 5 1 q.enqueue(1) x = q.dequeue() 3 x 5

Queue Illustrated q.enqueue(3) q.enqueue(2) q.enqueue(5) 2 5 1 q.enqueue(1) x = q.dequeue() 3 x y = q.dequeue() 6

Queue Illustrated q.enqueue(3) q.enqueue(2) q.enqueue(5) 5 1 q.enqueue(1) x = q.dequeue() 2 y y = q.dequeue() 7

Queue Illustrated q.enqueue(3) q.enqueue(2) q.enqueue(5) 5 1 6 4 q.enqueue(1) x = q.dequeue() y 2 y = q.dequeue() q.enqueue(6) q.enqueue(4) z = q.dequeue() 8

Queue Illustrated q.enqueue(3) q.enqueue(2) q.enqueue(5) 1 6 4 q.enqueue(1) x = q.dequeue() 5 z y = q.dequeue() q.enqueue(6) q.enqueue(4) z = q.dequeue() 9

Queue Illustrated q.enqueue(3) q.enqueue(2) q.enqueue(5) 1 6 4 q.enqueue(1) x = q.dequeue() z 5 y = q.dequeue() q.enqueue(6) q.enqueue(4) z = q.dequeue() 10

Queue Applications Queue applications often involve multiple users or processes and a shared resource, for example: A queue is used to hold print jobs submitted by users of a shared printer, while the printer services jobs one at a time Submitting a print job == enqueue Printing the next job == dequeue Communications software uses queues to hold data received over networks When data is transmitted to a system faster than it can be processed, it is stored in a queue as it is received Discrete Event Simulation Models the operation of a system as a sequence of events, each with a timestamp. The events are usually stored in a queue, often sorted/prioritized by timestamp. 11

Discrete Event Simulation For example, you could use discrete event simulation to study the queueing behavior of customers in line at the bank, waiting to be served by a bank teller Events to model: CustomerX arrives Teller begins service to CustomerX Randomize customer interarrival times and teller service times (using appropriate probability distribution) Choose number of tellers to model Sample outputs of simulation: Average wait time per customer Average line length Average teller busy/idle ratio 12

Queue Terminology Queue overflow The condition resulting from trying to add an element onto the rear of a full queue if(!q.isfull()) queue.enqueue(item); Queue underflow The condition resulting from trying to remove an element from an empty queue if(!q.isempty()) queue.dequeue(); 13

Queue Implementations Like stacks and other ADTs, a queue is only defined in terms of the properties of the data and the operations that can be performed on that data There are several data structures we could use to implement a Queue: Linked list (... once again, you've already done this one!) Enqueue == append node to list Dequeue == remove front node from list and return its value Fixed-size array Keep track of front and rear indexes; queue has fixed/maximum size and can be full Dynamic array Keep track of front and rear indexes; re-size/re-allocate when full 14

A Fixed-Size Array Queue Class class IntQueue { private: int *qarray; int qsize; // the queue array // the array size int front; // the front index in the queue int rear; // the last index in the queue int numitems; // the count of items in the queue public: IntQueue(int); // constructor IntQueue(const IntQueue &); // copy constructor ~IntQueue(); // destructor } void enqueue(int); int dequeue(); bool isfull() const; bool isempty() const; 15

Queue: Constructors IntQueue::IntQueue(int size) { if(size <= 0) size = 1; // or error } qarray = new int[size]; qsize = size; front = 0; // where is the front element located? rear = -1; // incremented before adding next element numitems = 0; IntQueue::IntQueue(const IntQueue &rhs) { qarray = new int[rhs.qsize]; qsize = rhs.qsize; for(int i = 0; i < qsize; i++) qarray[i] = rhs.qarray[i]; } front = rhs.front; rear = rhs.rear; 16

Queue: Destructor IntQueue::~IntQueue() { delete [] qarray; } 17

Array-Based Queue Implementation We know we need to track the front and rear indices of the array Add at rear index Remove element at front index When an item is dequeued from the front of the array, we DO NOT want to shift all elements over to fill the slot vacated by the dequeued item Why not? Instead, shift both front and rear indices through the array... 18

Array-Based Queue Implementation 15 3 7 2 4 6 8 front rear front front front rear rear rear rear q.enqueue(4) q.dequeue() q.dequeue() q.enqueue(6) q.dequeue() q.enqueue(8) q.enqueue(5) Where do we put the next element?! Wrap index around to the front of the array! When is the queue full? rear (wrapped) == front When is the queue empty? front (wrapped) == rear 19

Index Wrapping To wrap the rear index back to the front of the array, you can do the following: if(rear == qsize - 1) rear = 0; else rear = rear + 1; But this is equivalent (when 0 <= rear < qsize) and cleaner: rear = (rear + 1) % qsize; Do the same when advancing the front index 20

Queue: Enqueue void IntQueue::enqueue(int item) { assert(!isfull()); // queue overflow! // calculate the new rear position rear = (rear + 1) % qsize; // insert the new item qarray[rear] = item; } // update the item count numitems++; 21

Queue: Dequeue int IntQueue::dequeue() { assert(!isempty()); // queue underflow! // retrieve the front item int item = qarray[front]; // move front (increment by one) front = (front + 1) % qsize; // update the item count numitems--; } return item; 22

Queue: isfull & isempty bool IntQueue::isFull() { return (numitems == qsize); } bool IntQueue::isEmpty() { return (numitems == 0); } 23