Docsity

Material for Algorithms Midterm 2: Arrays and Linked Lists - Prof. James J. Fuller, Exams of Computer Science

Partial preview of the text

Download Material for Algorithms Midterm 2: Arrays and Linked Lists - Prof. James J. Fuller and more Exams Computer Science in PDF only on Docsity!

Material for Midterm Exam 2Material

for Midterm Exam 2Algorithms and Data StructuresAlgorithms and Data Structures

2/17/^

Material for Algorithms Midterm 2

From the course syllabus atFrom the course syllabus atFrom^ the course syllabus atFrom the course syllabus at

http://users.marshall.edu/~fullerj/Fall%202006/Algorithms/Software%20Engineering.pdfhttp://users.marshall.edu/~fullerj/Fall%202006/Algorithms/Software%20Engineering.pdf 2/17/^

Material for Algorithms Midterm 2

A ListA List

-^ So far we have considered two data structures •^ So far we have considered two data structures^ –^ Arrays^ –^ ArrayList

s

ArrayList

s

-^ We now consider the study of more general datastructures proscribed by the Java

interface List

Y^ ill^

th t^ A

Li t

i^ Li t

b t

-^ You will see that an

ArrayList

is a^ List

but an

array is not 2/17/^

Material for Algorithms Midterm 2

-^ Definition: A List is an expandable collection of elements in whicheach element has a position or index. In addition^ –^ We may access an element in a list in random order (oftencalled

random access

) by using the index

-^ We may access the elements in sequence using iterators • In Java an iterator is an implementation of the

Iterator

interface with the following methods

for Interface 2/17/^

Material for Algorithms Midterm 2

UML Diagram for theUML Diagram for the

ListList^

interfaceinterface Your next project

2/17/^

Material for Algorithms Midterm 2

Your next project

TheThe^ ArrayListArrayList

-^ We have worked with the

ArrayList

in many applications

We^ have worked with the

ArrayList

in many applications

-^ To declare an

ArrayList

, we simply say ArrayList <

name > = new ArrayList<

type >();

ArrayList

< name > = new ArrayList<

type >();

-^ If we omit <

type>, we get an

ArrayList

of Object.

-^ Some important methods of

ArrayList

that we already know

are^ –^ addadd^ –^ get^ –^ set^ –^ size 2/17/^

Material for Algorithms Midterm 2 size

Methods of

ArrayList

2/17/^

Material for Algorithms Midterm 2

Generic CollectionsGeneric Collections

-^ Java allows

Generic Collections

or simply^ Generics

-^ By a generic collection, we mean a collection that holds references toobjects of a certain type. •^ This type can be any Java class •^ We instantiate these collections with the required type

q^ yp

-^ The^ ArrayList

is an example

-^ A generic collection is declared as follows:^ CollectionClassName

identifier=

new

CollectionClassName(); 2/17/

Material for Algorithms Midterm 2

  • 2/17/
  • Material for Algorithms Midterm

Implementing the ArrayListImplementing the ArrayList

-^ By consulting the Java 5.0 API, we know the method signatures ofBy^ consulting the Java 5.0 API, we know the method signatures ofall^ ArrayList

methods.

-^ Someone

had to write the

ArrayList

methods that we use

Someone^ had

to write the

ArrayList

methods that we use.

-^ The authors show us one possible implementation in a class called^ KWArrayList

( KW =Koffman and Wolfgang authors of the text) KWArrayList

( KW =Koffman and Wolfgang, authors of the text)

-^ The authors use an array to implement their version of ArrayList •^ The following slides shows why an array implementation may beinefficient. 2/17/^

Material for Algorithms Midterm 2

Remarks about previous slideRemarks about previous slide

-^ Since the

ArrayList

will be implemented using an array, an array is a private member variable of the

KWArrayList

-^ Note that the array holds objects of type

E.^ E^ is a place holder and

represents any object type. • The originial array

capacity

(using the

length^ member

variable of arrays) is 0 but is set to 10 in the constructor 2/17/^

Material for Algorithms Midterm 2

The add method.

Notice the generic E This method is summarized on slide 19. ItThis method is summarized on slide 19. Itallows theallows the^ KWArrayListKWArrayList

to growto grow.

allows theallows the^ KWArrayListKWArrayList

to growto grow.

This method adds the element in the specified position.Adding at the end is a simpler implementation and is shownat the top of the slide.

Material for Algorithms Midterm 2

//get(i) methodpublic E get(int index){ if(index<0 || index>=size)throw new ArrayIndexOutOfBoundsException);elsereturn theData[index];} public void reallocate{ // set capacity to 2capacity// set capacity to 2*

capacity //Declare an array of type E with new capacity//copy data into the new array//copy data into the new array// have the data array point to the new array} 2/17/^

Material for Algorithms Midterm 2 }

Linked ListsLinked Lists

-^ We now turn our attention to Linked Lists •^ It will overcome a few deficiencies of the array implementation of^ ArrayList

class

-^ When we add an element to an Array, we are performing an

y,^ p

g

O(n) operation since we have to shift (on average) n/2 elements down^ in the list – When we delete an element from an Array, we are performingO( )^

ti^ i^

h^ t^ hift (

)^ /

an O(n) opereation since we have to shift (on average) n/2elements^ up

in the list

-^ Finding the proper place to insert a new element is O(n) • The linked list makes this an O(1) operation i.e. it occurs inconstant time if we know where to put it. Locating the correctposition is O(n) 2/17/^

Material for Algorithms Midterm 2 position^ is O(n)