problem of the day two missiles speed directly toward each other one goes 9,000 miles per hour ...
TRANSCRIPT
Problem Of The Day
Two missiles speed directly toward each other One goes 9,000 miles per hour Other goes 21,000 miles per hour.
If they start 1,317 miles apart, how far apart are they 1 minute before colliding?
Problem Of The Day
Two missiles speed directly toward each other One goes 9,000 miles per hour Other goes 21,000 miles per hour.
If they start 1,317 miles apart, how far apart are they 1 minute before colliding?
They are closing at 30,000MPH ; 1 minute of this is:
30,000/60 = 3000/6 = 500 miles!
LECTURE 29:POSITION LIST
CSC 212 – Data Structures
INDEXLIST ≠ array
Extends array concepts, like using indices, but… INDEXLISTs do not have constant size Element’s index changes as data added &
removed ADTs remain completely
implementation-free Using non-tenured faculty still perfectly
acceptable
INDEXLIST.add()
add(i, e) “shifts” elements to make space Traverse linked list or move array elements
Can take O(n) time for this shifting Worst case occurs in both array- & linked
list-based May only take O(1) to add to end of list
0 1 2
e
n-1i
INDEXLIST.remove()
remove(i) “shifts” elements down to fill hole Not implementation-specific – only indices
are shifted O(n) time required in the general case
But for specific situations could take only O(1) time
But must consider worst case when computing big-Oh0 1 2
e
n-1i
INDEXLIST
DEQUE QUEUE STACK
add()addFront()addLast()
enqueue() push()
get()getFront()getLast()
front() top()
remove()removeFront()removeLast()
dequeue() pop()
set() None! None! None!
Operations To Date
Collection which we can access all elements Add element before an existing one Return the Collection’s 3rd element Loop over all elements without removing
them LIST ADTs differ in how they provide
access INDEXLIST uses indices for absolution
positioning Can only use relative positions in NODELIST
List ADT
Does Absolute Position Matter?
Relativity Can Be Important
Based upon a conceptual linked list No implementation specified: still an ADT Relies upon Nodes implementing POSITION
ADT POSITIONs at relative locations in the
LIST first() & last() POSITIONs only
absolutes Traverse using next(p) & prev(p) methods
POSITIONLIST with 3 elements
NODELIST ADT
first last
Abstracts Node idea for use anywhere Get the POSITION's value using element() Cannot change element using interface's
methods Position is ADT & works with array elements class ArrayPos<E> implements Position<E> {
private E[] theArray;private int index;
public ArrayPos(E[] arr, int idx) { … }
public E element(){ return theArray[index]; }
}
POSITION Interface
public interface PositionList<E> extends Collection {Position<E> first();Position<E> last();Position<E> next(Position<E> p) throws /* … */;Position<E> prev(Position<E> p) throws /* … */;
E set(Position<E> p, E elem) throws /* … */; E remove(Position<E> p) throws /* … */;
void addFirst(E elem);void addLast(E elem);void addBefore(Position<E> p, E e) throws/*…*/;void addAfter(Position<E> p, E e) throws /* …*/;
}
Memorization Limits
Good news: remove & set the same Now take POSITION as parameter and not an
index Whatever it had as its value before call returned
Relative location used to add elements to LIST addFirst & addLast are relative to all others e in new POSITION before p via addBefore addNext creates new POSITION after p for e
NODELIST Methods
Linked list implements NODELIST in most cases This is not required, but is most reasonable
Few methods free with doubly-linked lists Use next & prev fields to move between
POSITIONs Call Node.setElement(e) to implement set
Most other methods we've already implemented Just adding & removing Nodes from the
linked list
Implementing NODELIST
Linked list implements NODELIST in most cases This is not required, but is most reasonable
Few methods free with doubly-linked lists Use next & prev fields to move between
POSITIONs Call Node.setElement(e) to implement set
Most other methods we've already implemented Just adding & removing Nodes from the
linked list
But how to do this with POSITIONs?
Implementing NODELIST
99% of typecasts are incorrect 90% fix missing generic type specification
on variable Eclipse “Quick-Fix” on illegal code is 9% 0.95% instantiate arrays of generic type When using interfaces in an ADT is 0.05%
Typecasting Explained
99% of typecasts are incorrect 90% fix missing generic type specification
on variable Eclipse “Quick-Fix” on illegal code is 9% 0.95% instantiate arrays of generic type When using interfaces in an ADT is 0.05%
Typecasting Explained
Used when we implement PositionList Class allocates instance, so typecasting is
safe
private Node<E> checkPosition(Position<E> p) throws InvalidPositionException {if ((p == null) || !(p instanceof Node)) { throw new InvalidPositionException();}return (Node<E>)p;
}
From Position to Node
Your Turn
Get into your groups and complete activity
For Next Lecture
Before Wednesday’s lecture read GT 6.3 What is an Iterator? How is it used? How are Iterable classes related to Lists? Are Iterable & Iterators related? If so,
how?
Programming Assignment #2 available now javadoc is your friend; please write good
comments