Download presentation
Presentation is loading. Please wait.
Published byEdgar Waters Modified over 9 years ago
1
CSC 212 Sequences & Iterators
2
Announcements Midterm in one week Will cover through chapter 5 of book Midterm 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
3
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 rank public interface Sequence extends List, Vector { public Position atRank(int r) throws public int rankOf(Position p) throws }
4
Implementing Sequences Like all ADTs, Sequences do not require any specific implementation We discuss 2 implementations today: Array-based Sequences Doubly linked list-based Sequences
5
Linked-list base Sequence Node class implements Position interface Makes position based calls simple Using/finding rank requires searching through list nodes/positions elements
6
Array-based Sequence Ranks are easy to use But, must create class implementing Position Use array in circular manner Just like with queues 0123 positions elements S lf
7
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:
8
Iterator ADT Often process data in a collection in order Position defines current element But need collection to get next item Cannot easily determine when end is reached Cannot iterate through data without accessing the list/sequence Limits modularity & reusability
9
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?
10
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();
11
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
12
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
13
Review Material for the Midterm More topics: Writing/Using Linked Lists Stacks & Queues Differences between LIFO and FIFO Vectors, Lists, & Sequences How rank and position works Iterators
14
Ideas for Reviewing (Re-)Read book sections you do not understand Look over daily quizzes and the solutions Make sure you understand why the answers are what they are
15
Ideas for Reviewing Will post practice midterm on course webpage Will update with more questions on Saturday Practice contains questions that could be on an actual midterm Also posting (separate) answer key
16
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 students Provides a good focus for your studying
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.