chapter 3 adts unsorted list and sorted list. 3-2 list definitions linear relationship each element...

65
Chapter 3 ADTs unsorted List and Sorted List

Upload: daniella-marr

Post on 30-Mar-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

Chapter 3

ADTs unsorted List and Sorted List

Page 2: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-2

List Definitions

Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique successor.

Length The number of items in a list; the length can vary over time.

Page 3: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-3

List Definitions

Unsorted list A list in which data items are placed in no particular order; the only relationship between data elements is the list predecessor and successor relationships.

Sorted list A list that is sorted by the value in the key; there is a semantic relationship among the keys of the items in the list.

Key The attributes that are used to determine the logical order of the list.

Page 4: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-4

Assumptions for All our Lists

1. Our lists are composed of unique elements.

2. When sorted, our lists are sorted from the smallest to largest key value.

3. We use the “by copy” approach.

4. We use the programming “by contract” approach.

Page 5: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

Development of an Unsorted List ADT:

UnsortedStringList

Page 6: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-6

Unsorted List ADT Specification

Page 7: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-7

Constructors

Page 8: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-8

Observers

Page 9: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-9

Transformers

Page 10: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-10

Iterators

Page 11: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-11

Application Level

Page 12: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-12

List Design Terminology

Page 13: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-13

Instance Variables of Unsorted List ADT

Page 14: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-14

Beginning of Unsorted StringList Class:

Page 15: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-15

Constructors

Page 16: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-16

Definitions

• Signature The distinguishing features of a method heading. The combination of a method name with the number and type(s) of its parameters in their given order.

• Overloading The repeated use of a method name with a different signature.

Page 17: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-17

Simple Observers

public boolean isFull ( )

// Returns whether this lis is full

{

return (list.length == numItems);

}

Page 18: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-18

isThere Operation

Page 19: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-19

Retrieving an Item in an Unsorted List

Page 20: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-20

Retrieving an Item in an Unsorted List

Page 21: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-21

Page 22: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-22

insert Operation

Page 23: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-23

Deleting Bobby (move up)

• public void delete (String item)• // Deletes the element that matches item from this list• {• int location = 0;• while (item.compareTo(list[location]) != 0)• location++;• If(isThere(item)• {• For( int I = location; i,<numItm-1;i++)• list[i] = list[i+1];• }• }

Page 24: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-24

Deleting Bobby (swap)—more efficient

• public void delete (String item)• // Deletes the element that matches item from this list• {• int location = 0;• while (item.compareTo(list[location]) != 0)• location++;• list[location] = list[numItems - 1];• numItems--;• }

Page 25: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-25

Page 26: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-26

UML Diagram of UnsortedStringList

Page 27: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-27

Reuse Operations

Ways we could reuse the code of the Unsorted List ADT to create the code for the Sorted List ADT:

1. Cut and Paste—”cut and paste” the code that we are able to reuse into the new file.

2. Direct Inheritance—have the Sorted List ADT inherit methods from the Unsorted List ADT.

3. Abstract Classes—resolve the deficiencies of both of the previous approaches.

Page 28: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-28

Steps for Using Abstract Class Approach

1. We first create an abstract list class.1. Its concrete methods provide the operations that

our two list ADTs share in common.

2. Its abstract methods provide the operations that are not shared.

2. Then create two concrete classes that extend the abstract list class.1. One that implements an unsorted list

2. The other that implements a sorted list

Page 29: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-29

The Abstract Class

• Please click on the following link Programs/C03P165.jpg to view the appropriate program.

Page 30: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-30

Extending the Abstract Class

• Please click on the following link Programs/C03P166.jpg to view the appropriate program.

Page 31: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-31

UML Diagram

Page 32: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

Abstract Data Type Sorted List

Page 33: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-33

Sorted List ADT Specification (partial)

Page 34: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-34

Constructors

Page 35: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-35

Redefined insert

Page 36: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-36

Redefined Delete

Page 37: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-37

insert Operation

1. Find the place where the new element begins.

2. Create space for the new element.

3. Put the new element on the list.

Page 38: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-38

Original List

Page 39: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-39

Insert Becca

Page 40: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-40

Result

Page 41: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-41

insert (item)

Page 42: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-42

Page 43: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-43

delete (item)

Page 44: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-44

Page 45: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-45

Binary Search Algorithm

Page 46: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-46

isThere (item): returns boolean

Page 47: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-47

Page 48: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-48

Page 49: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-49

Page 50: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-50

Page 51: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-51

Page 52: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-52

UML Diagram

• Please click on the following link Programs/C03P180.jpg to view the appropriate program.

Page 53: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-53

Comparison of Algorithms

Big-O Notation A notation that expresses computing time (complexity) as the term in a function that increases most rapidly relative to the size of a problem

Iff(N) = N4 + 100N2 + 10N + 50

then f(N) is 0(N4). N represents the size of the problem.

Page 54: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-54

Comparison of Rates of Growth

Page 55: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-55

Comparison of Linear and Binary Searches

Page 56: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-56

Big-O Comparison of List Operations

Page 57: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-57

Generic ADTsSo far…• An unsorted list of strings• An unsorted list of strings that extended

String List• A sorted list of strings that extended

String ListNext—• Lists of generic dataGeneric Data Type A type for which the

operations are defined but the types of the items being manipulated are not

Page 58: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-58

The Listable Interface

Page 59: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-59

A ListCircle Class

Page 60: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-60

A Generic Abstract List Class

• Please click on the following link Programs/C03P196.jpg to view the appropriate program.

Page 61: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-61

Page 62: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-62

A Generic Sorted List ADT

• Please click on the following link Programs/C03P200.jpg to view the appropriate program.

Page 63: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-63

UML Diagrams for our List Framework

Page 64: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-64

A Listable Class

• Please click on the following link Programs/C03P204.jpg to view the appropriate program.

Page 65: Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each

3-65

Using the Generic List

To Create a sorted list of strings use either of its constructors:

SortedList list1 = new SortedList();SortedList list2 = new SortedList (size);

Declare at least one object of class ListStringListString aString;

Instantiate ListString objects and place them on the list.

aString = new ListString(“Amy”);list.insert(astring)