Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X.

Slides:



Advertisements
Similar presentations
List Implementations That Use Arrays
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
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.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures and Abstractions with Java, 4e Frank Carrano
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
April 24, 2017 Chapter 4 Data Abstraction The Walls
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, 
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
An Array-Based Implementation of the ADT List
Data Abstraction: The Walls
Lists Chapter 4.
Bag Implementations that Use Arrays
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
A Bag Implementation that Links Data
Object Based Programming
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Iterators (short version)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Adapted from Pearson Education, Inc.
Slides by Steve Armstrong LeTourneau University Longview, TX
Dynamic Data Structures and Generics
List Implementations that Use Arrays
Dynamic Data Structures and Generics
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Bag Implementations that Use Arrays
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations that Use Arrays
A List Implementation that Uses An Array
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Chapter Contents Specifications for the ADT List Using the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending Machine Java Class Library: The Interface List

Specifications for the ADT List A list provides a way to organize data Fig. 4-1 A to-do list.

Specifications for the ADT List Operations on lists Add new entry – at end, or anywhere Remove an item Remove all items Replace an entry Look at any entry Look for an entry of a specific value Count how many entries Check if list is empty, full Display all the entries

Specifications for the ADT List To specify an ADT list Describe its data Specify the operations ADT list must be considered in general Not necessarily a list of strings View specifications

Fig. 4-2 The effect of ADT list operations on an initially empty list Example Fig. 4-2 The effect of ADT list operations on an initially empty list

Potential Problem Operations add, remove, replace, getEntry work OK when valid position given remove, replace and getEntry not meaningful on empty lists A list could become full, what happens to add?

Possible Solutions Assume the invalid situations will not occur Ignore the invalid situations Make reasonable assumptions, act in predictable way Return boolean value indicating success or failure of the operation Throw an exception

Redefining Specifications A first draft of an ADT specifications may ignore potential problems Simplifies the first draft Concentrate on details after major portions of specifications written Makes the specifications complete After writing specifications, implementations Write Java statements to use the ADT Checks understanding, suitability of the specifications

Interface for ADT ListInterface View source code Note Interface has no data fields, constructors Methods must be public Strategy for add, remove, replace, getEntry is to have them return a value Use of return of a reference in remove and getEntry

Using the ADT List Fig. 4-3 A list of numbers that identify runners in the order in which they finish a race

Using the ADT List Consider the scoring of a running race We wish to note the order in which the runners finish We add each runner's (unique) number to the end of the list When done we display the whole list View sample program

A List is Like a Vending Machine Fig 4-4 A vending machine.

A List is Like a Vending Machine Observations about vending machines Can perform only tasks shown on interface Must understand the tasks Cannot see inside the machine Can use the machine even though don’t know what happens inside If inside of machine replaced with new improved version Interface remains unchanged Customer uses machine in same way as before

A List is Like a Vending Machine Observations about clients and List ADT Client can perform only operations from the ADT List Client must adhere to specifications Client cannot access data without an ADT operation Client can use the list – even though unable to access entries directly If implementation is changed, client still uses list in same way as before

Java Class Library: The Interface List The standard package contains a list interface – called List Methods provided

List Implementations That Use Arrays Chapter 5 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

An Analogy Consider a classroom with 40 desks in fixed position Desks are wasted if less than 40 students Not enough desks if more than 40 students An array is like the classroom Each desk an array location

Fig. 5-1 A classroom that contains desks in a fixed position. An Analogy Fig. 5-1 A classroom that contains desks in a fixed position.

An Analogy Suppose we have some students in classroom in order alphabetically We add a new student We desire to maintain the alphabetic order We must shift some students We remove a student in the middle of the sequence Again, we must shift some students

Adding a Student Fig. 5-2 Seating a new student between two existing students: at least one other student must move

The Java Implementation Private data fields for implementation of AList Implements interface ListInterface of Chapter 4 View full specification source code

AList add() Methods First add method adds a new item at the end of the list Assign new value at end Increment length of list Second add method adds item in mid-list Requires a utility method, makeRoom() This shifts elements ahead

Adding Items in Mid-list Fig. 5-3 Making room to insert Carla as third entry in an array.

Adding Items in Mid-list Fig. 5-4 An array of objects contains references to those objects Note: figures in this text portray arrays as if they actually contained objects

The remove() Method View method remove() Must shift existing entries to avoid gap in the array – note method removeGap() Except when removing last entry Method must also handle error situations When position specified in the remove is invalid When remove() is called and the list is empty Invalid call returns null value

Fig. 5-5 Removing Bob by shifting array entries. Removing a List Entry Fig. 5-5 Removing Bob by shifting array entries.

Other Methods in AList Note other methods in the class Note implements java.io.Serializable Tells compiler that instances of AList can be written to a file using object serialization

Expanding an Array An array has a fixed size When array becomes full If we need a larger list, we are in trouble When array becomes full Move its contents to a larger array (dynamic expansion) Copy data from original to new location Manipulate names so new location keeps name of original array

Expanding an Array Fig. 5-6 The dynamic expansion of an array copies the array's contents to a larger second array.

Expanding an Array Fig. 5-7 (a) an array; (b) the same array with two references; (c) the two arrays, reference to original array now referencing a new, larger array

Expanding an Array Code to accomplish the expansion shown in Fig. 5-7, previous slide

A New Implementation of a List Change the isFull to always return false We will expand the array when it becomes full We keep this function so that the original interface does not change The add() methods will double the size of the array when it becomes full Now declare a private method isArrayFull Called by the add() methods Click to view these methods

Java Class Library Has two classes that use dynamic array expansion ArrayList Vector Both classes Found in java.util Implement interface List Defined in terms of a generic type

Using a Vector to Implement the ADT List Java's Vector class provides capabilities of an array Able to expand dynamically Hides the details of the process Vector Has methods for manipulating entries Enables implementing the ADT List

Using a Vector Fig. 5-8 A client uses the methods given in ListInterface, but the implementation of the list uses Vector methods to perform its operations

Using a Vector View elements of the class definition VectorList <T> Note Constructors add methods replace remove getEntry

Using a Vector The add() methods The remove() method The first uses the addElement method from the Vector class The other uses the insertElementAt method The remove() method Uses the removeElementAt method

Pros and Cons of Array Use for the ADT List When using an array or vector … Retrieving an entry is fast Adding an entry at the end of the list is fast Adding or removing an entry that is between other entries requires shifting elements in the array Increasing the size of the array or vector requires copying elements