Algorithms and OOD (CSC 207 2014F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [Learning Outcomes] [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Student-Curated Resources] [Java 8 API] [Java 8 Tutorials] [Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2014S (Rebelsky)] [CSC 207 2014F (Walker)] [CSC 207 2011S (Weinman)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Overview
** Missing: Way too many students: RHB
previous method breaks our implementation of
add and, if so, how you fix it.remove twice in sequence?nextnext? It makes insertion much easier.previous OperationHere's a typical implementation of the previous operation.
public T previous()
throws NoSuchElementException
{
failFast();
if (!this.hasPrevious())
throw new NoSuchElementException();
Node current = SimpleLinkedList.this.front;
while (current.next != this.cursor)
current = current.next;
this.cursor = current;
this.pos--;
return this.cursor.next.data;
} // previous()
Another strategy: You can use the position field to identify how many times you have to move forward.
--this.pos;
for (i = 0; i < this.pos; i++)
current = current.next;
How do you implement hasPrevious?
See if it's equal to the dummy node. Approximately ...
public boolean hasPrevious()
{
return this.cursor == SimpleLinkedList.this.front;
}
Or use the position field.
Discuss with partner:
next Seems okayhasNext Seems okaypreviousIndex IgnorenextIndex Ignoreset - set a value (replace previous value)
Sets the value most recently returned by next or previousadd - Does this break?previous break? How do we make set work?
ListIterator<String> it = list.listIterator()
...
// State Hello Goodbye Sam
// ^
it.set("x");
// What happens? If you just called `previous`,
// when we call set we get
// State Hello X Sam
// In contrast, if you just called `next`,
// when we call set, we get
// State X Goodbye Sam
One strategy: Store a pointer to the prior element.
// State: alpha beta gamma
// ^
// We know that we've moved forward.
it.remove()
What should happen conceptually? Remove the beta
// State: alpha ^ gamma
How do we achieve that goal?
previous method.it.remove() immediately thereafter?Thoughts
previous - back up to
the beginning of the list and find the node before the one we want
do remove. Discuss with partner:
remove twice in sequence?Discuss with partner
Node class?next node. Where should we keep it now?add?remove?next?
*`previous?previous method, we had to store additional
information. Do we need to store the same information?