csc 212 sequences & iterators. announcements midterm in one week will cover through chapter 5...
TRANSCRIPT
CSC 212
Sequences & Iterators
Announcements
Midterm in one weekWill cover through chapter 5 of bookMidterm will be open book, open note (but,
closed computer) Will not be at office hours tomorrow
Or at next 3 lectures (lucky you!)Will hold all-day office hours Tuesday
Sequences
Sequences combine List and Vector ADTs Also enables conversion between the two
atRank – returns the position object for a given rank
rankOf – given a position, returns its rankpublic interface Sequence extends List, Vector {public Position atRank(int r) throws •••
public int rankOf(Position p) throws •••
}
Implementing Sequences
Like all ADTs, Sequences do not require any specific implementationWe discuss 2 implementations today:
Array-based Sequences Doubly linked list-based Sequences
Linked-list base Sequence
Node class implements Position interfaceMakes position based calls simpleUsing/finding rank requires searching through
list
nodes/positions
elements
Array-based Sequence
Ranks are easy to useBut, must
create class implementing Position
Use array in circular mannerJust like with
queues
0 1 2 3
positions
elements
S
lf
Group Work
public class Node implements Position { protected Object elem; protected Node prev; protected Node next; ... }
public class LLSequence implements Sequence { protected Node head; protected int size; ...}
public class ArrPos implements Position { protected Object elem; protected int rank;
...
}public class ArrSequence implements Sequence { protected ArrPos arr[]; protected int first; protected int last;
...}
Write atRank(r) & rankOf(p) for each class:
Iterator ADT
Often process data in a collection in orderPosition defines current element But need collection to get next itemCannot easily determine when end is reached
Cannot iterate through data without accessing the list/sequenceLimits modularity & reusability
Iterator Interface
public interface Iterator {boolean hasNext();Object next();
} Must call next() to get initial item
Future calls to next() step through items Should either of these throw an exception?
Getting Iterators
Extend the List (and therefore Sequence) ADT with two methods:
/** Iterate through the * positions in the list */public Iterator positions();/** Iterate through elements in * the list */public Iterator elements();
Using Iterators
Loops can now rely on using Iterators Improves modularity
As long as new collection provides an iterator, can change ADT used for collection at will
Improves reuse Code relies only on iterator existence and not on
any specific implementation details
Review Material for the Midterm
Topics we have discussed: Programming in Java Class hierarchy and inheritance
Field hiding, method overriding, method chaining What assignments are legal and when is typecasting needed
Big-Oh notation Constant, Linear, and Logarithmic time
Recursion
Review Material for the Midterm
More topics:Writing/Using Linked ListsStacks & Queues
Differences between LIFO and FIFO
Vectors, Lists, & Sequences How rank and position works Iterators
Ideas for Reviewing
(Re-)Read book sections you do not understand
Look over daily quizzes and the solutionsMake sure you understand why the answers
are what they are
Ideas for Reviewing
Will post practice midterm on course webpage Will update with more questions on SaturdayPractice contains questions that could be on
an actual midtermAlso posting (separate) answer key
Daily Quiz
Due Friday at 4PM E-mail me a question you think would be
good for the midterm and its solution I usually include questions from studentsProvides a good focus for your studying