![Page 1: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/1.jpg)
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?
![Page 2: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/2.jpg)
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!
![Page 3: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/3.jpg)
LECTURE 29:POSITION LIST
CSC 212 – Data Structures
![Page 4: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/4.jpg)
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
![Page 5: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/5.jpg)
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
![Page 6: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/6.jpg)
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
![Page 7: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/7.jpg)
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
![Page 8: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/8.jpg)
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
![Page 9: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/9.jpg)
Does Absolute Position Matter?
![Page 10: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/10.jpg)
Relativity Can Be Important
![Page 11: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/11.jpg)
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
![Page 12: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/12.jpg)
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
![Page 13: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/13.jpg)
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
![Page 14: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/14.jpg)
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
![Page 15: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/15.jpg)
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
![Page 16: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/16.jpg)
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
![Page 17: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/17.jpg)
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
![Page 18: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/18.jpg)
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
![Page 19: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/19.jpg)
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
![Page 20: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/20.jpg)
Your Turn
Get into your groups and complete activity
![Page 21: 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](https://reader035.vdocuments.net/reader035/viewer/2022062309/5697bfc61a28abf838ca70d0/html5/thumbnails/21.jpg)
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