Download presentation
Presentation is loading. Please wait.
Published byMervin Doyle Modified over 9 years ago
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 1,317 miles apart, how far apart are they 1 minute before colliding?
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 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!
3
CSC 212 – Data Structures
4
I NDEX L IST ≠ array Extends array concepts, like using indices, but… I NDEX L IST s 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
5
I NDEX L IST.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 012 e n-1 i
6
I NDEX L IST.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-Oh 012 e n-1 i
7
I NDEX L IST D EQUE Q UEUE S TACK add() addFront() addLast() enqueue()push() get() getFront() getLast() front()top() remove () removeFront() removeLast() dequeue()pop() set() None! Operations To Date
8
Collection which we can access all elements Add element before an existing one Return the Collection’s 3 rd element Loop over all elements without removing them L IST ADTs differ in how they provide access I NDEX L IST uses indices for absolution positioning Can only use relative positions in N ODE L IST List ADT
9
Does Absolute Position Matter?
10
Relativity Can Be Important
11
Based upon a conceptual linked list No implementation specified No implementation specified: still an ADT Relies upon Node s implementing P OSITION ADT P OSITION s at relative locations in the L IST first() & last() P OSITION s only absolutes Traverse using next(p) & prev(p) methods P OSITION L IST with 3 elements N ODE L IST ADT first last
12
Abstracts Node idea for use anywhere Get the P OSITION 's value using element() Cannot change element using interface's methods Position is ADT & works with array elements class ArrayPos implements Position { private E[] theArray; private int index; public ArrayPos(E[] arr, int idx) { … } public E element(){ return theArray[index]; } } P OSITION Interface
13
public interface PositionList extends Collection { Position first(); Position last(); Position next(Position p) throws /* … */ ; Position prev(Position p) throws /* … */ ; E set(Position p, E elem) throws /* … */ ; E remove(Position p) throws /* … */ ; void addFirst(E elem); void addLast(E elem); void addBefore(Position p, E e) throws /*…*/ ; void addAfter(Position p, E e) throws /* …*/ ; } Memorization Limits
14
Good news: remove & set the same Now take P OSITION as parameter and not an index Whatever it had as its value before call returned Relative location used to add elements to L IST addFirst & addLast are relative to all others e in new P OSITION before p via addBefore addNext creates new P OSITION after p for e N ODE L IST Methods
15
Linked list implements N ODE L IST 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 P OSITION s Call Node. setElement(e) to implement set Most other methods we've already implemented Just adding & removing Node s from the linked list Implementing N ODE L IST
16
Linked list implements N ODE L IST 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 P OSITION s Call Node. setElement(e) to implement set Most other methods we've already implemented Just adding & removing Node s from the linked list But how to do this with P OSITION s? Implementing N ODE L IST
17
Typecasting Explained
19
Used when we implement PositionList Class allocates instance, so typecasting is safe private Node checkPosition(Position p) throws InvalidPositionException { if ((p == null) || !(p instanceof Node)) { throw new InvalidPositionException(); } return (Node )p; } From Position to Node
20
Your Turn Get into your groups and complete activity
21
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 List s? Are Iterable & Iterator s related? If so, how? Programming Assignment #2 Programming Assignment #2 available now good javadoc is your friend; please write good comments
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.