Title: ADTs unsorted List and Sorted List
1Chapter 3
- ADTs unsorted List and Sorted List
2List 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.
3List 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.
4Assumptions for All our Lists
- Our lists are composed of unique elements.
- When sorted, our lists are sorted from the
smallest to largest key value. - We use the by copy approach.
- We use the programming by contract approach.
5Development of an Unsorted List ADT
UnsortedStringList
6Unsorted List ADT Specification
7Constructors
8Observers
9Transformers
10Iterators
11Application Level
12List Design Terminology
13Instance Variables of Unsorted List ADT
14Beginning of Unsorted StringList Class
15Constructors
16Definitions
- 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.
17Simple Observers
- public boolean isFull ( )
- // Returns whether this lis is full
-
- return (list.length numItems)
18isThere Operation
19Retrieving an Item in an Unsorted List
20Retrieving an Item in an Unsorted List
21(No Transcript)
22insert Operation
23Deleting Bobby (move up)
- public void delete (String item)
- // Deletes the element that matches item from
this list -
- int location 0
- while (item.compareTo(listlocation) ! 0)
- location
- If(isThere(item)
-
- For( int I location i,ltnumItm-1i)
- listi listi1
-
24Deleting Bobby (swap)more efficient
- public void delete (String item)
- // Deletes the element that matches item from
this list -
- int location 0
- while (item.compareTo(listlocation) ! 0)
- location
- listlocation listnumItems - 1
- numItems--
25(No Transcript)
26UML Diagram of UnsortedStringList
27Reuse Operations
- Ways we could reuse the code of the Unsorted List
ADT to create the code for the Sorted List ADT - Cut and Pastecut and paste the code that we
are able to reuse into the new file. - Direct Inheritancehave the Sorted List ADT
inherit methods from the Unsorted List ADT. - Abstract Classesresolve the deficiencies of both
of the previous approaches.
28Steps for Using Abstract Class Approach
- We first create an abstract list class.
- Its concrete methods provide the operations that
our two list ADTs share in common. - Its abstract methods provide the operations that
are not shared. - Then create two concrete classes that extend the
abstract list class. - One that implements an unsorted list
- The other that implements a sorted list
29The Abstract Class
- Please click on the following link
Programs/C03P165.jpg to view the appropriate
program.
30Extending the Abstract Class
- Please click on the following link
Programs/C03P166.jpg to view the appropriate
program.
31UML Diagram
32Abstract Data Type Sorted List
33Sorted List ADT Specification (partial)
34Constructors
35Redefined insert
36Redefined Delete
37insert Operation
- Find the place where the new element begins.
- Create space for the new element.
- Put the new element on the list.
38Original List
39Insert Becca
40Result
41insert (item)
42(No Transcript)
43delete (item)
44(No Transcript)
45Binary Search Algorithm
46isThere (item) returns boolean
47(No Transcript)
48(No Transcript)
49(No Transcript)
50(No Transcript)
51(No Transcript)
52UML Diagram
- Please click on the following link
Programs/C03P180.jpg to view the appropriate
program.
53Comparison 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 - If
- f(N) N4 100N2 10N 50
- then f(N) is 0(N4).
- N represents the size of the problem.
-
54Comparison of Rates of Growth
55Comparison of Linear and Binary Searches
56Big-O Comparison of List Operations
57Generic ADTs
- So far
- An unsorted list of strings
- An unsorted list of strings that extended String
List - A sorted list of strings that extended String
List - Next
- Lists of generic data
- Generic Data Type A type for which the
operations are defined but the types of the items
being manipulated are not
58The Listable Interface
59A ListCircle Class
60A Generic Abstract List Class
- Please click on the following link
Programs/C03P196.jpg to view the appropriate
program.
61(No Transcript)
62A Generic Sorted List ADT
- Please click on the following link
Programs/C03P200.jpg to view the appropriate
program.
63UML Diagrams for our List Framework
64A Listable Class
- Please click on the following link
Programs/C03P204.jpg to view the appropriate
program.
65Using 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 ListString
- ListString aString
- Instantiate ListString objects and place them on
the list. - aString new ListString(Amy)
- list.insert(astring)