COP 3530: Data Structures, Algorithms, - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

COP 3530: Data Structures, Algorithms,

Description:

General purpose implementation of linear lists. ... It's the Cadillac version of our ArrayLinearListWithIterator. Next Time in COP 3530... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 35
Provided by: root83
Category:

less

Transcript and Presenter's Notes

Title: COP 3530: Data Structures, Algorithms,


1
COP 3530 Data Structures, Algorithms,
Applications
  • Instructor Kristian Linn Damkjer

2
Linear List Implementation
  • ArrayLinearList

3
About ArrayLinearList
  • General purpose implementation of linear lists.
  • Describes one way of creating the Data Structure
    Linear List
  • Implements the LinearList interface
  • May contain methods in addition to those
    specified in LinearList

4
Creating New Lists
  • All lists are initially empty
  • This is simply by design, not a limitation
  • What must we consider?
  • Use of data structure in program
  • Initial size of array
  • Examples
  • ArrayLinearList a new ArrayLinearList(100),
  • b new ArrayLinearList(),
  • c
  • LinearList d new ArrayLinearList(100),
  • e new ArrayLinearList(),
  • f

5
Oppsie!
  • ArrayLinearList a new LinearList(100),
  • b new LinearList(),
  • c
  • LinearList d new LinearList(100),
  • e new LinearList(),
  • f

WRONG
6
Using Linear Lists
  • Should not depend on the implementation
  • Example
  • System.out.println(a.size())
  • a.add(0, new Integer(2))
  • b.add(0, new Integer(4))
  • System.out.println(a)
  • b.remove(0)
  • if (a.isEmpty())
  • a.add(0, new Integer(5))

7
Why keep it generic?
  • Consider an array of Linear Lists
  • By declaring the array as type LinearList, we may
    store any instances of any implementation we
    wish.
  • Example
  • LinearList x new LinearList4
  • x0 new ArrayLinearList(20)
  • x1 new Chain()
  • x2 new Chain()
  • x3 new ArrayLinearList()
  • for (int i 0 i lt 4 i)
  • xi.add(0, new Integer(i))

8
Linear List Implementation
  • Class Structure

9
Skeletal Structure
  • / array implementation of LinearList /
  • package dataStructures
  • import java.util. // Java utilities package
    for Iterator
  • import utilities. // Our utilities package
    for resizing
  • public class ArrayLinearList implements
    LinearList
  • // data members
  • protected Object element // array of
    elements
  • protected int size // number of
    elements in array
  • // constructors
  • // methods

10
Linear List Implementation
  • Constructors

11
Constructor
  • /
  • Create a list with initial capacity
    initialCapacity
  • _at_throws IllegalArgument Exception when
    initialCapacity lt 1
  • /
  • public ArrayLinearList(int initialCapacity)
  • if (initialCapacity lt 1)
  • throw new IllegalArgumentException(
  • "initialCapacity must be gt
    1")
  • // size has the default initial value of 0
  • element new ObjectinitialCapacity

12
No-Argument Constructor
  • /
  • Create a list with initial capacity 10
  • /
  • public ArrayLinearList()
  • // use the default capacity of 10
  • this(10)

13
Linear List Implementation
  • Methods

14
isEmpty()
  • / _at_return true if and only if the list is empty
    /
  • public boolean isEmpty()
  • return size 0

15
size()
  • / _at_return current number of elements in the
    list /
  • public int size()
  • return size

16
checkIndex()
  • / _at_throws IndexOutOfBoundsException /
  • void checkIndex(int index)
  • if (index lt 0 index gt size)
  • throw new IndexOutOfBoundsException(
  • "index " index "size "
    size)

17
get()
  • /
  • _at_return element with specified index
  • _at_throws IndexOutOfBoundsException when index
    is not
  • between 0 and size - 1
  • /
  • public Object get(int index)
  • checkIndex(index)
  • return elementindex

18
indexOf()
  • /
  • _at_return index of first occurrence of
    theElement,
  • return -1 if theElement is not in the
    list
  • /
  • public int indexOf(Object theElement)
  • // search element for theElement
  • for (int i 0 i lt size i)
  • if (elementi.equals(theElement))
  • return i
  • // return -1 if theElement was not found
  • return -1

19
remove()
  • /
  • Remove the element with specified index and
    update indices
  • _at_throws IndexOutOfBoundsException when index
    is not
  • between 0 and size - 1
  • _at_return removed element
  • /
  • public Object remove(int index)
  • checkIndex(index)
  • // valid index, shift elements with higher
    index
  • Object removedElement elementindex
  • for (int i index 1 i lt size i)
  • elementi 1 elementi
  • element--size null // enable garbage
    collection
  • return removedElement

20
add()
  • /
  • Insert an element with specified index. All
    elements with
  • equal or higher index have their index
    increased by 1.
  • _at_throws IndexOutOfBoundsException when index
    is not
  • between 0 and size
  • /
  • public void add(int index, Object theElement)
  • if (index lt 0 index gt size)
  • // invalid list position
  • throw new IndexOutOfBoundsException(
  • "index " index "size "
    size)

21
add()
  • // valid index, make sure we have space
  • if (size element.length)
  • // if no space, double capacity
  • element ChangeArrayLength.changeLength1D(e
    lement,

  • 2 size)
  • // shift elements right one position
  • for (int i size 1 i gt index i--)
  • elementi 1 elementi
  • // insert the element and increase size
  • elementindex theElement
  • size

22
Faster Shift
  • System.arraycopy(
  • // original array
  • element,
  • // starting index in original
  • index,
  • // target array
  • element,
  • // starting index in target
  • index 1,
  • // number of elements to copy
  • size index
  • )

23
toString()
  • / convert to a String /
  • public String toString()
  • StringBuffer s new StringBuffer("")
  • // put elements into the buffer
  • for (int i 0 i lt size i)
  • if (elementi null)
  • s.append("null, ")
  • else
  • s.append(elementi.toString() ", ")
  • if (size gt 0)
  • s.delete(s.length() 2, s.length())
    //remove last ", "
  • s.append("")
  • // create equivalent String
  • return new String(s)

24
Iterators
  • Definition

25
What is an iterator?
  • An iterator facilitates the iterative examination
    of data structure elements
  • We will often need to examine all elements in a
    data structure
  • Repeated get operations usually have a lot of
    unnecessary overhead
  • Not all structures have a get behavior
  • Java provides Iterator as an interface

26
Iterator
  • Methods

27
Creating Iterators
  • Contrary to what youre used to, iterators are
    generally not instantiated directly.
  • Iterator ix new IteratorImplementation()
  • Instead you should use the iterator method which
    must be defined for Iterator implementations
  • Iterator ix myObject.iterator()

DONT DO THIS
28
What can iterators do?
  • Iterators are very simple, they only have three
    behaviors in addition to iterator
  • They can correctly identify whether or not there
    is an element immediately after the current
    element
  • They can tell us what the next element is, if
    there is one
  • Has the side-effect of making the next element
    the currently examined element
  • They may be able to remove the current element,
    though this is not always guaranteed

29
Behavior Details
  • The Iterator method hasNext determines the
    existence of a next element.
  • Returns true if and only if there is a next
    element
  • The Iterator method next identifies the next
    element if it exists.
  • Throws NoSuchElementException if there is no next
    element
  • Returns and advances to the next element otherwise

30
Optional Behavior
  • The Iterator method remove removes the last
    element that was returned by next
  • remove is not necessarily supported
  • Throws UnsupportedMethodException if the method
    is not implemented
  • Throws IllegalStateException if next has not been
    called or did not return an element
  • Removes the last element returned by next
    otherwise

31
Using Iterators
  • What youre used to doing
  • for (int i 0 i lt x.size() i)
  • doSomthingWith(x.get(i))
  • Now with an iterator
  • Iterator ix x.iterator()
  • while(ix.hasNext())
  • doSomethingWith(ix.next())

32
Why Use Iterators?
  • It is often possible to implement next so that
    its complexity is less than that of get
  • Many data structures do not have a get behavior
  • Iterators provide a uniform way to step through
    the elements of a data structure

33
What Would Java Do?
  • java.util.ArrayList
  • Its the Cadillac version of our
    ArrayLinearListWithIterator

34
Next Time in COP 3530
  • Link-Based Representation of Linear List
  • Link-Based Implementation of Linear List (a.k.a.
    Chain)
  • Read Chapter 6
  • Yes, all of it
Write a Comment
User Comments (0)
About PowerShow.com