Chapter 3
This presentation is the property of its rightful owner.
Sponsored Links
1 / 65

Chapter 3 PowerPoint PPT Presentation


  • 45 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 3. ADTs unsorted List and Sorted List. 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. List Definitions.

Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 3

Chapter 3

ADTs unsorted List and Sorted List


List definitions

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.


List definitions1

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.


Assumptions for all our lists

Assumptions 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.


Development of an unsorted list adt unsortedstringlist

Development of an Unsorted List ADT: UnsortedStringList


Unsorted list adt specification

Unsorted List ADT Specification


Constructors

Constructors


Observers

Observers


Transformers

Transformers


Iterators

Iterators


Application level

Application Level


List design terminology

List Design Terminology


Instance variables of unsorted list adt

Instance Variables of Unsorted List ADT


Beginning of unsorted stringlist class

Beginning of Unsorted StringList Class:


Constructors1

Constructors


Definitions

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.


Simple observers

Simple Observers

public boolean isFull ( )

// Returns whether this lis is full

{

return (list.length == numItems);

}


Isthere operation

isThere Operation


Retrieving an item in an unsorted list

Retrieving an Item in an Unsorted List


Retrieving an item in an unsorted list1

Retrieving an Item in an Unsorted List


Insert operation

insert Operation


Deleting bobby move up

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];

  • }

  • }


Deleting bobby swap more efficient

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--;

  • }


Uml diagram of unsortedstringlist

UML Diagram of UnsortedStringList


Reuse operations

Reuse Operations

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

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

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

  • Abstract Classes—resolve the deficiencies of both of the previousapproaches.


Steps for using abstract class approach

Steps 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


The abstract class

The Abstract Class

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


Extending the abstract class

Extending the Abstract Class

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


Uml diagram

UML Diagram


Abstract data type sorted list

Abstract Data Type Sorted List


Sorted list adt specification partial

Sorted List ADT Specification (partial)


Constructors2

Constructors


Redefined insert

Redefined insert


Redefined delete

Redefined Delete


Insert operation1

insert Operation

  • Find the place where the new element begins.

  • Create space for the new element.

  • Put the new element on the list.


Original list

Original List


Insert becca

Insert Becca


Result

Result


Insert item

insert (item)


Delete item

delete (item)


Binary search algorithm

Binary Search Algorithm


Isthere item returns boolean

isThere (item): returns boolean


Uml diagram1

UML Diagram

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


Comparison of algorithms

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

If

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

then f(N) is 0(N4).

N represents the size of the problem.


Comparison of rates of growth

Comparison of Rates of Growth


Comparison of linear and binary searches

Comparison of Linear and Binary Searches


Big o comparison of list operations

Big-O Comparison of List Operations


Generic adts

Generic 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


The listable interface

The Listable Interface


A listcircle class

A ListCircle Class


A generic abstract list class

A Generic Abstract List Class

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


A generic sorted list adt

A Generic Sorted List ADT

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


Uml diagrams for our list framework

UML Diagrams for our List Framework


A listable class

A Listable Class

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


Using the generic list

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 ListString

ListString aString;

Instantiate ListString objects and place them on the list.

aString = new ListString(“Amy”);

list.insert(astring)


  • Login