Algorithms and OOD (CSC 207 2013F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Overview
Admin
Prepend
public void prepend(T val) {
// Create a new node, with T as a val and front as next
Node<T> newfront = new Node(T, this.front);
this.front = newfront;
} // prepend(T)
public void prepend(T val) {
// Create a new node, with T as a val and front as next
this.front = new Node(T, this.front);
} // prepend(T)
Two strategies:
Code, revisited
Node<T> front;
Node<T> back;
public void prepend(T val) {
// Create a new node, with T as a val and front as next
this.front = new Node(T, this.front);
} // prepend(T)
public void append(T val) {
// Create a new node with val and no successor
// Update the former back of the list to make this new node the next node
this.back.next = new Node(T, null);
// Update our notion of the back of the list
this.back = this.back.next;
} // append(T)
What do we initialize front and back to for an empty list?
null!
public void prepend(T val) { // Create a new node, with T as a val and front as next this.front = new Node(T, this.front); // Special case: Empty list if (this.back == null) this.back = this.front; } // prepend(T)
public void append(T val) { // Deal with empty list if (this.back == null) { this.back = new Node(T, null); this.front = this.back; } else { // Create a new node with val and no successor // Update the former back of the list to make this new node the next node this.back.next = new Node(T, null); // Update our notion of the back of the list this.back = this.back.next; } } // append(T)
Worrying about special cases in linked lists is really important! (Was it empty? for insert; Will it be empty? for delete)
On to insert and delete with a cursor
public class LinkedListCursor<T> implements Cursor {
} // LinkedListCursor<T>
Add after current element is easy, delete current element is hard
Deletion
cursor.prev.next = cursor.prev.next.next;
A problem: How do we delete the first element, and where is cursor.prev when we start?
Twice the links! Twice the fun! Twice the potential for errors.
Dummy node with link to front and back
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Copyright (c) 2013 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by/3.0/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.