Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Thursday, April 25, 13

Similar documents
Notes on access restrictions

Interfaces & Generics

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

182 review 1. Course Goals

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

Examination Questions Midterm 1

Java Review: Objects

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

Introduction to Programming Using Java (98-388)

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Linked Structures. See Section 3.2 of the text.

Curriculum Map Grade(s): Subject: AP Computer Science

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

csci 210: Data Structures Stacks and Queues

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

Introduction to Computing II (ITI 1121) Final Examination

package weiss.util; // Fig , pg // Fig 6.16,6.17, pg package weiss.util;

CISC 323 Intro to Software Engineering

CE204 Data Structures and Algorithms Part 2

Data Structure. Recitation VII

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

ITI Introduction to Computing II

Top Down Design vs. Modularization

CS171 Midterm Exam. October 29, Name:

CS/CE 2336 Computer Science II

CSC Advanced Object Oriented Programming, Spring Specification

Apply to be a Meiklejohn! tinyurl.com/meikapply

Computer Programming II Python

CS 221 Review. Mason Vail

Chapter 4 Defining Classes I

CSE 331 Midterm Exam 2/13/12

Functors. 01/23/03 Lecture 5 1

Chapter 18: Stacks And Queues

JAVA CONCEPTS Early Objects

COMP 250 Midterm #2 March 11 th 2013

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Topic 14. The BinaryTree ADT

G51PRG: Introduction to Programming Second semester

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

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/22/04 Lecture 4 1

Introduction to Computing II (ITI 1121) Final Examination

Queues Fall 2018 Margaret Reid-Miller

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Queue. COMP 250 Fall queues Sept. 23, 2016

Computer Science 62. Midterm Examination

Outline. iterator review iterator implementation the Java foreach statement testing

Generics. IRS W-9 Form

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

cs Java: lecture #6

CS 314 Midterm 2 Fall 2012

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

+ Abstract Data Types

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

School of Computing and Information Sciences. Course Title: Data Structures Date: 3/30/2010 Course Number: COP 3530 Number of Credits: 3

tree nonlinear Examples

CSC212:Data Structure

Lecture 10 Notes Linked Lists

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Chapter 18: Stacks And Queues

Computer Programming II C++ (830)

CS18000: Programming I

Contracts. Dr. C. Constantinides. June 5, Department of Computer Science and Software Engineering Concordia University Montreal, Canada 1/71

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

Classes, interfaces, & documentation. Review of basic building blocks

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Java Interfaces. 6 April 2016 OSU CSE 1

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Queues. Stacks and Queues

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

ITI Introduction to Computing II

Midterm Exam 2 CS 455, Spring 2011

Recap. List Types. List Functionality. ListIterator. Adapter Design Pattern. Department of Computer Science 1

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

CSI33 Data Structures

Announcements Queues. Queues

CT 229 Object-Oriented Programming Continued

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

The following topics will be covered in this course (not necessarily in this order).

IMACS: AP Computer Science A

Stacks and queues. CSCI 135: Fundamentals of Computer Science Keith Vertanen.

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Lecture 9 Stacks & Queues

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

CS61B Lecture #19. Last modified: Mon Oct 13 12:02: CS61B: Lecture #19 1

Randomized Queues and Deques

Linked Lists. Chapter 12.3 in Savitch

In this lecture Simple List Algorithms

Summer Final Exam Review Session August 5, 2009

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

Data Structures and Algorithms Notes

CMPS 240 Data Structures and Algorithms Test #2 Fall 2017 November 15

Transcription:

Queue Implemented with 1 2 numitems = enqueue (); 1 Queue Implemented with 2 numitems = 2 enqueue (); enqueue (); 2 Queue Implemented with 2 numitems = 2 enqueue (); enqueue (); enqueue (5); enqueue (6); enqueue (7); enqueue (8);

Queue Implemented with 5 6 7 8 numitems = 6 enqueue (); enqueue (); enqueue (5); enqueue (6); enqueue (7); enqueue (8); Queue Implemented with 5 6 7 8 numitems = 6 enqueue (); enqueue (); enqueue (5); enqueue (6); enqueue (7); enqueue (8); enqueue (9); 5 Queue Implemented with 9 5 6 7 8 numitems = 7 enqueue (); enqueue (); enqueue (5); enqueue (6); enqueue (7); enqueue (8); enqueue (9); 6

Data Members for Circular Array int ; // index of front of queue int ; // index of element of queue int numitems; // how many in queue int capacity; // how many queue can hold T[] values; // array for data 7 Implementing a Circular Array boolean isempty() { return numitems == 0; int size() { return numitems; 8 Implementing a Circular Array T getfirst() { return values[]; T removefirst() { if (numitems > 0) { T returnvalue = values[]; = ( + 1) % capacity; numitems--; return returnvalue return null; 9

Implementing a Circular Array void addlast (T item) { if (numitems == capacity) { reallocate(); // Like ArrayLists, grow if necessary = ( + 1) % capacity; values[] = item; numitems++; 10 Growing a Circular Array 9 10 5 6 7 8 Where should the data go? 9 10 5 6 7 8 11 Growing a Circular Array 9 10 5 6 7 8 Where should the data go? 5 6 7 8 9 10 12

Growing a Circular Array void reallocate() { int newcapacity = 2 * capacity; T[] newdata = new T[newCapacity]; int j = ; for (int i = 0; i < numitems; i++) { newdata[i] = values[j]; j = (j+1) % capacity; = 0; = numitems - 1; capacity = newcapacity; values = newdata; 1 Queue Abstraction Data structure that removes items in the same order they are added public interface Queue<T> { public void enqueue (T item); // Add an item to end public T front(); // Get item waiting the longest public T dequeue(); // Remove item waiting the longest public boolean isempty(); // Are any items waiting? public int size(); // How many items are waiting? Circular Array?? Linked List?? Abstraction! You can use a queue without knowing how it is implemented! 1 Queue in Collection Hierarchy <<interface>> Iterable <<interface>> Collection <<interface>> Queue <<interface>> List <<abstract>> AbstractCollection <<abstract>> AbstractList LinkedList ArrayList Stack 15

What was this course about anyway??? Assuring Correctness Algorithm Performance Algorithmic Problem Solving Fundamentals!! Program Decomposition Programming Concepts Java/Eclipse/JUnit Basic Data Structures Magic! NOT! 16 Java Topics Classes: methods, constructors, instance variables Contracts: method signatures, assert statements Interfaces, polymorphism, inheritance Swing - basics of painting, using components, attaching listeners Scope: instance variables, parameters, local variables Exception handling Basics of File I/O Iterators 17 Software Development Topics Contracts: preconditions and postconditions Recursion 18

Data Structure Topics O() analysis ArrayList Linked lists Stacks Queues Should know: How each represents data Methods supported by each class O() costs of those methods 19 Skills Knowing Java terminology: constructor, exception, object, class,... Reading Java code to determine what it does Recognizing good / bad style in a Java program Recognizing whether Java code follows the Java s subtyping rules Understanding a stack trace Writing very small amounts of Java code Showing how you would call a method given its signature or contract Identifying good test cases for a method Doing a O() analysis Designing a recursive algorithm 20 Summer Ideas Keep practicing! Still uneasy about Java? Head First Java Want to challenge yourself? Program an Android or iphone! Learn a new language, like Python! Read a book! Final Jeopardy - about Watson Effective Java Blown to Bits Linked: The New Science Of Networks Ted Talks, for example... What is the Internet, Really? The rise of human-computer cooperation 21

NullPointerException http://xkcd.com/71/ Object variables 22