Problem of the Day Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST Winner is one who owns SLOWEST horse. 1 st two weeks, race is aimless mess, never to end Missing their families, kids consult a wise man Upon leaving the wise man, kids jump on horses Race at top speed Race at top speed to the city. What did the wise man say?
Problem of the Day Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST Winner is one who owns SLOWEST horse. 1 st two weeks, race is aimless mess, never to end Missing their families, kids consult a wise man Upon leaving the wise man, kids jump on horses Race at top speed Race at top speed to the city. What did the wise man say? ride the other's horse! He told them to ride the other's horse!
CSC 212 – Data Structures
All elements available when stored in a List Without removing a thing can access all elements Add element anywhere in the List For existing location, set changes element in-place List s implement Iterable interface iterator() returns Iterator over elements Instance of 2 nd class returned by this method Iterable makes it easy to access elements Use for-each to handle any Iterable instance List ADT
But List s still differ in how they are organized To use ArrayList need absolute index (an int ) Positions (references) used by NodeList Ideally have ADT with both sets of methods Could use both absolute & relative ordering Conversion methods enable jumping back-and-forth Identical Access w/ Iterable?
But List s still differ in how they are organized To use ArrayList need absolute index (an int ) Positions (references) used by NodeList Ideally have ADT with both sets of methods Identical Access w/ Iterable?
But List s still differ in how they are organized To use ArrayList need absolute index (an int ) Positions (references) used by NodeList Ideally have ADT with both sets of methods Identical Access w/ Iterable?
But List s still differ in how they are organized To use ArrayList need absolute index (an int ) Positions (references) used by NodeList Ideally have ADT with both sets of methods Identical Access w/ Iterable?
Sequence ADT Combines D EQUE, I NDEX L IST, & P OSITION L IST Includes all methods defined by these interfaces Adds 2 methods to convert between systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position
Sequence ADT Combines D EQUE, I NDEX L IST, & P OSITION L IST Includes all methods defined by these interfaces Adds 2 methods to convert between systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position
Sequence ADT Combines D EQUE, I NDEX L IST, & P OSITION L IST Includes all methods defined by these interfaces Adds 2 methods to convert between systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position
Sequence ADT Combines D EQUE, I NDEX L IST, & P OSITION L IST Includes all methods defined by these interfaces Adds 2 methods to convert between systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position
interface Sequence extends IndexList, PositionList, Deque { public Position atIndex(int r) throws BoundaryViolationException; public int indexOf(Position p) throws InvalidPositionException; } As ADT, must be implementation independent Specify Position okay, but not it's implementation array linked list S EQUENCE class free to use array or linked list S EQUENCE Interface
Actually used by all of Java’s List classes Combines everything so far into one simple idea Can serve as basis of S TACK, Q UEUE, or D EQUE Small, simple databases built from Sequence s Basic building block for many other structures Many use S EQUENCE somewhere in implementation Need to know how Position & index works Use big-Oh costs to select which implementation Why Use a Sequence?
Actually used by all of Java’s List classes Combines everything so far into one simple idea Can serve as basis of S TACK, Q UEUE, or D EQUE Small, simple databases built from Sequence s Basic building block for many other structures Many use S EQUENCE somewhere in implementation Need to know how Position & index works Use big-Oh costs select which implementation Use big-Oh costs to select which implementation Why Use a Sequence?
Easier & faster if nodes doubly-linked Position -based methods come from N ODE L IST To find indices, need to traverse Node s Could use Node s via getNext() & getPrev() next() & prev() if want Position traversal Linked list-based Sequence Node s List
Implementing atIndex(i) Node s List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
Suppose call was: atIndex(3) i 3 trav Implementing atIndex(i) Node s List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
Suppose call was: atIndex(3) i 3 trav Implementing atIndex(i) Node s List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
Node s Suppose call was: atIndex(3) i 3 2 trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
Node s Suppose call was: atIndex(3) i trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
Node s Suppose call was: atIndex(3) i trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
Position Suppose call was: atIndex(3) i trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
Node s Suppose call was: atIndex(3) i trav Implementing atIndex(i) List Algorithm atIndex(i) if (i 0) do trav next(trav) i-- endwhile return trav
Array-based Sequence Based on IndexList Use and resize array Easy to implement Store elements in array S 0123 N-1 List
Array-based Sequence S 0123 N-1 List
Array-based Sequence Array of Position s Can be used in methods Position contains: Element Index in array No next or prev links Instead use the index Update index when Position shifted 0123 S 0123 N-1 List Position
Array-based Sequence Algorithm next(pos) if (pos == S[size()-1]) then throw BoundaryViolation… else idx pos.getIndex() return S[idx+1] fi end 0123 S 0123 N-1 List Position
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 012 S 0123 N-1 List Position
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 013 S 0123 N-1 List Position
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 023 S 0123 N-1 List Position
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 123 S 0123 N-1 List Position
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 123 S 0123 N-1 List Position 0
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ for i size() downto 1 do S[i] S[i-1] S[i].setIndex(i) endfor S[0] new Node(0, e) size++ end 123 S 0123 N-1 List Position 0
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ for i size() downto 1 do S[i] S[i-1] S[i].setIndex(i) endfor S[0] new Node(0, e) size++ end 123 S 0123 N-1 List Position 0
Your Turn Get into your groups and complete activity
For Next Lecture