Download presentation
Presentation is loading. Please wait.
Published byEstella Fields Modified over 9 years ago
1
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?
2
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!
3
CSC 212 – Data Structures
4
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
5
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?
6
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?
7
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?
8
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?
9
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
10
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
11
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
12
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
13
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
14
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?
15
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?
16
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
17
Implementing atIndex(i) Node s List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
18
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
19
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
20
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
21
Node s Suppose call was: atIndex(3) i 3 2 1 trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
22
Node s Suppose call was: atIndex(3) i 3 2 1 0 trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
23
Position Suppose call was: atIndex(3) i 3 2 1 0 trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi
24
Node s Suppose call was: atIndex(3) i 3 2 1 0 trav Implementing atIndex(i) List Algorithm atIndex(i) if (i 0) do trav next(trav) i-- endwhile return trav
25
Array-based Sequence Based on IndexList Use and resize array Easy to implement Store elements in array S 0123 N-1 List
26
Array-based Sequence S 0123 N-1 List
27
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
28
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
29
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 012 S 0123 N-1 List Position
30
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 013 S 0123 N-1 List Position
31
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 023 S 0123 N-1 List Position
32
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 123 S 0123 N-1 List Position
33
Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 123 S 0123 N-1 List Position 0
34
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
35
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
36
Your Turn Get into your groups and complete activity
37
For Next Lecture
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.