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
Discuss with partner
What changes do we make to the Node class to achieve doubly-linked
lists?
public class Node
{
T data;
Node next;
Node prev;
} // class Node
In dealing with singly-linked lists, we kept the cursor at the
node before the next node. Where should we keep it now?
add?remove?next?previous?set?previous method, we had to store additional
information (e.g., which direction we are going). Do we need to store
the same information?remove method, we had to keep track of whether we'd
just removed a node. Should we still do so?Question one: Where do we put the cursor?
next, just like we did before.
(aka "right before the conceptual cursor")
next value, because that seems more
sensible. (aka "right after the conceptual cursor")
Let's write pseudo-Java (not all of the syntax will be perfect
public class DoublyLinkedIterator<T>
{
Node<T> cursor;
int pos;
boolean forward;
public T next()
{
failFast();
if (!this.hasNext())
throw new SomeSillyThingException()
this.cursor = this.cursor.next;
forward = true;
return this.cursor.data;
} // next()
public T previous()
{
failFast();
if (!this.hasPrevious())
throw new SomeSillyThingException()
this.cursor = this.cursor.previous;
forward = false;
return this.cursor.next.data;
} // previous()
public void add(T val)
{
failFast();
Node<T> newnode = new Node(this.cursor, val, this.cursor.next); // (prev,val,next)
this.cursor.next = newnode;
newnode.next.prev = newnode;
this.cursor = newnode;
} // add(T)
public void remove()
public void set(T val)
}