CS 2430 Day 12. Agenda for today Container class example: DateList Growable container classes.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Stacks, Queues, and Linked Lists
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CS 2430 Day 14. Agenda for today The Bag class No mixing! What if we have a bunch of String s that we want to store in our program? We can’t use the.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Arrays An array is a collection of variables, all of the same type. An array is created with the new operator. The size of an array must be specified at.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Linked Lists CSC 172 SPRING 2002 LECTURE 3 Agenda  Average case lookup – unsorted list & array  Amortized insert for array  Sorted insert  Binary.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
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.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
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 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
CS 2430 Day 26. Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via .
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
CS1101: Programming Methodology Recitation 6 – Arrays and Collections.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Object-Oriented Programming Simple Stack Implementation.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday at 10pm Program 4 grace date: Wednesday at 10pm (don’t.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
A: A: double “4” A: “34” 4.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
Iterator Summary Slides by Entesar Al-Mosallam adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing an ADT Example: Sum-Constraint ADT Part II Ohad Barzilay May 2004.
An Array-Based Implementation of the ADT List
Linked Data Structures
Week 3 - Friday CS221.
Stacks.
Refactoring Methods: Kevin Murphy.
A Bag Implementation that Links Data
null, true, and false are also reserved.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
CSE 373: Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Simple Classes in Java CSCI 392 Classes – Part 1.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks CS-240 Dick Steflik.
Presentation transcript:

CS 2430 Day 12

Agenda for today Container class example: DateList Growable container classes

List of Date s ADT Data: Default maximum size (constant) Array of Date s Current number of Date s in container Operations: Add a date Remove a date Size Contains a date? Item at (ordered list)

Class DateList

public class DateList { public static final int DEFAULT_NUM = 10; private Date[] dates; private int size; public DateList() {... } public DateList(int inSize) {... } public int size() {... } public boolean add(Date inDate) {... } public boolean remove(Date inDate) {... } public boolean contains(Date inDate) {... } public Date itemAt(int index) {... } // is DateList ordered?... // we can add other methods later }

Implementation of class DateList

Constructors

public class DateList { public static final int DEFAULT_NUM = 10; private Date[] dates; private int size; public DateList() { dates = new Date[DEFAULT_NUM]; size = 0; } public DateList(int inSize) { if (inSize < 0) dates = new Date[DEFAULT_NUM]; else dates = new Date[inSize]; size = 0; }... }

public class DateList {... public boolean add(Date inDate) { if (size >= dates.length) // can't use DEFAULT_NUM -- WHY? return false; dates[size++] = inDate; return true; }... } Implementing add()

public class DateList {... private int find(Date inDate) { for (int i = 0; i < size; i++) if (dates[i].equals(inDate)) return i; return -1; }... } Implementing find()

public class DateList {... private int find(Date inDate) { for (int j = 0, k = size – 1; k > 0; k = k / 2, j++) { for (int i = j; i < size; i += k) { if (dates[i].equals(inDate)) return i; } } return -1; }... } Weird find()

public class DateList {... private int find(Date inDate) {... } public boolean remove(Date inDate) // if DateList is ordered { int index = find(inDate); if (index < 0) return false; for (int i = index; i < size – 1; i++) dates[i] = dates[i + 1]; size--; return true; }... } Implementing remove()

public class DateList {... private int find(Date inDate) {... } public boolean remove(Date inDate) // if DateList if not ordered { int index = find(inDate); if (index < 0) return false; dates[index] = dates[size - 1]; size--; return true; }... } Another remove()

public class DateList {... private int find(Date inDate) {... } public boolean contains(Date inDate) { return find(inDate) >= 0; }... } Implementing contains()

public class DateList {... public Date itemAt(int index) // only if DateList is ordered { if (index = size) return null; return dates[index]; }... } Implementing itemAt()

Any questions?

What happens when we run out of room in the DateList ?

It should be a “growable” DateList !

public class DateList { public static final int DEFAULT_NUM = 10; private Date[] dates; private int size; public DateList() {... } public DateList(int inSize) {... } public int size() {... } public boolean add(Date inDate) {... } public boolean remove(Date inDate) {... } public boolean contains(Date inDate) {... } public Date itemAt(int index) {... }... }

public class GrowableDateList { public static final int GROWBY = 10; // change constructors! private Date[] dates; private int size; public GrowableDateList() {... } public GrowableDateList(int inSize) {... } public int size() {... } public boolean add(Date inDate) {... } public boolean remove(Date inDate) {... } public boolean contains(Date inDate) {... } public Date itemAt(int index) {... } private void grow() {... }... }

New method: grow() private void grow() { // how to increase size of array dates? Date temp[] = new Date[size + GROWBY]; for (int i = 0; i < size; i++) temp[i] = dates[i]; // don't forget: dates = temp; }

Cannot grow public boolean add(Date inDate) { if (size >= dates.length) return false; dates[size++] = inDate; return true; } Before

add() uses grow() public boolean add(Date inDate) { if (size >= dates.length) return false; dates[size++] = inDate; return true; } Before public boolean add(Date inDate) { if (size >= dates.length) grow(); dates[size++] = inDate; return true; } Now!

Watching arrays grow() private void grow() { // how to increase size of array dates? Date temp[] = new Date[size + GROWBY]; for (int i = 0; i < dates.length; i++) temp[i] = dates[i]; // don't forget: dates = temp; } 1/2/937/8/992/4/96 dates ► ► ► ► ► i 1/2/93 3/4/95 7/8/99 1/3/95 2/4/96

When does add() return false ?

Never!

Any questions?