CSC 1052 Algorithms & Data Structures II: Queues

Similar documents
CSC 1052 Algorithms & Data Structures II: Linked Queues

CSC 1052 Algorithms & Data Structures II: Stacks

CSC 1052 Algorithms & Data Structures II: Recursion

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

CSC 1052 Algorithms & Data Structures II: Linked Lists

CSC 1052 Algorithms & Data Structures II: Lists

CSC 1052 Algorithms & Data Structures II: Linked Lists Revisited

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Stack and Queue. Stack:

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

ITI Introduction to Computing II

CSC 1052 Algorithms & Data Structures II: Interfaces

CSC 1052 Algorithms & Data Structures II: Avoiding Recursion

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

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

Data Structure. Recitation VII

Basic Data Structures

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

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

CS 261: Data Structures. Dynamic Array Queue

Basic Data Structures 1 / 24

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

LIFO : Last In First Out

Apply to be a Meiklejohn! tinyurl.com/meikapply

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

csci 210: Data Structures Stacks and Queues

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

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

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

CSEN 301 Data Structures and Algorithms


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

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Stacks and Queues III

Stacks, Queues (cont d)

More Data Structures (Part 1) Stacks

Chapter 18: Stacks And Queues

CS24 Week 4 Lecture 2

Lecture 2: Stacks and Queues

" Which data Structure to extend to create a heap? " Two binary tree extensions are needed in a heap. n it is a binary tree (a complete one)

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

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

CS350: Data Structures Stacks

CSE 214 Computer Science II Heaps and Priority Queues

Chapter 18: Stacks And Queues

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

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

STACKS AND QUEUES. Problem Solving with Computers-II

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Queues. Queue ADT Queue Implementation Priority Queues

Queue rear tail front head FIFO

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

Lecture 3: Stacks & Queues

Announcements Queues. Queues

Your Topic Goes Here Queue

An Introduction to Queues With Examples in C++

Linear Data Structures

Interfaces & Generics

CSE 143 SAMPLE MIDTERM

Queue with Array Implementation

CS 261. Dynamic Array Queue by Tim Budd Ron Metoyer Sinisa Todorovic

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

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

.:: UNIT 4 ::. STACK AND QUEUE

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

Lecture Data Structure Stack

Week 4 Stacks & Queues

Priority Queue. How to implement a Priority Queue? queue. priority queue

Queues Fall 2018 Margaret Reid-Miller

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

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CS 216 Exam 1 Fall SOLUTION

Ashish Gupta, Data JUET, Guna

ADTs Stack and Queue. Outline

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

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

Midterm Exam (REGULAR SECTION)

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Lecture 2: Implementing ADTs

Stacks and Queues. CSE 373 Data Structures Lecture 6

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

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

1 P age DS & OOPS / UNIT II

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Data Abstraction and Specification of ADTs

Stacks and Queues. David Greenstein Monta Vista

ADTs Stack and Queue. Outline

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

Computer Science 501 Data Structures & Algorithms The College of Saint Rose Fall Topic Notes: Linear Structures

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

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

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

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

Data Structures G5029

Transcription:

CSC 1052 Algorithms & Data Structures II: Queues Professor Henry Carter Spring 2018

Recap Recursion solves problems by solving smaller version of the same problem Three components Applicable in a range of scenarios Not a silver bullet: typically associated with space inefficiency

New ADT: Queues Waiting in line common in real life and computing Data is often processed in the order that it is received A queue provides first-in, first-out data ordering (FIFO) Compare to LIFO data structure

Example Queues Network packets (buffering) Compute jobs/processes Support ticketing system

Abstract: The Queue Interface Two canonical operations for adding and removing items Enqueue and Dequeue Helper operations size isempty isfull

Implementing the Queue Create your project Import files Open the interface

Interface Code package ch04.queues; public interface QueueInterface<T> { void enqueue(t element) throws QueueOverflowException1; // Throws QueueOverflowException if this queue is full; // otherwise, adds element to the rear of this queue. T dequeue() throws QueueUnderflowException; // Throws QueueUnderflowException if this queue is empty; // otherwise, removes front element from this queue and returns it. boolean isfull(); // Returns true if this queue is full; // otherwise, returns false. boolean isempty(); // Returns true if this queue is empty; // otherwise, returns false. } int size(); // Returns the number of elements in this queue.

Implementation: Array Store items in an array Some questions to consider: How do we track the front and back? How large should the queue be? What are the tradeoffs between different techniques?

ArrayBoundedQueue Fixed length queue Will require similar overflow/underflow exceptions to our stack Front and back indices Remaining operations will mirror the stack implementation

Preparation Create your ArrayBoundedQueue class file Open the interactive test driver Try out a few tests (they should all fail!)

Implement! Class variables Constructors Interface methods

Analysis Enqueue: Dequeue: Helpers:

How do we improve dequeue? Functionally, we don't have to start the queue at the beginning of the array Track both the beginning and end of the queue elements What happens when we hit the end?

Queue wrapping

Queue wrapping

Queue wrapping code Modulo operator allows us to wrap the index Adds complexity to the code Significantly reduces order of growth What is our new OOG?

Implement! Modify the ArrayBoundedQueue class to use queue wrapping

Breaking boundaries: unbounded queues Functionally the same as a bounded queue Except that the queue is never full Primitive arrays are fixed-length in Java What do we need to add to get this unbounded functionality?

Enlarge() method Creates a new array What size should the array be? Copies elements Careful to preserve ordering in case of wrapping Replace elements array with new array Swap the pointer!

Enlarge code private void enlarge() // Increments the capacity of the queue by an amount // equal to the original capacity. { // create the larger array T[] larger = (T[]) new Object[elements.length + origcap]; // copy the contents from the smaller array into the larger array int currsmaller = front; for (int currlarger = 0; currlarger < numelements; currlarger++) { larger[currlarger] = elements[currsmaller]; currsmaller = (currsmaller + 1) % elements.length; } } // update instance variables elements = larger; front = 0; rear = numelements - 1;

Cost Enlarge worst-case order of growth This OOG is passed on to enqueue Amortized cost helps us assess the cost of operations that are only performed occasionally If we make many calls to enqueue before enlarging, the cost of one enlarge is amortized Evaluated in many different ways Statistically Worst-case Empirically

Recap A queue simulates a waiting line, where objects are removed in the same order they are added The primary operations are: Enqueue Dequeue Size, isempty, isfull Array-based implementation requires several technical tradeoffs Fixed front vs floating front Fixed size vs expanding size

Next Time... Dale, Joyce, Weems Chapter 4.5 Remember, you need to read it BEFORE you come to class! Check the course webpage for practice problems Peer Tutors http://www.csc.villanova.edu/help/ 23