[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Back to Linked Lists. On to Object-Oriented Design (1).
Held Monday, March 15
Summary
Contents
Handouts
Notes
static
when declaring static methods.)
/**
* Nodes for a singly-linked list.
*
* @author Samuel A. Rebelsky
* @version 1.1 of March 1999
*/
public class Node {
/**
* The value in the current node. A value of null
* indicates (...)
*/
protected Object value;
/**
* The next node in the list. A value of null
* indicates that this is the last element of the list.
*/
protected Node next;
...
} // Node
null, for links from nodes that
have no next element.
tail, which now needs to create new list
objects.
/**
* A singly-linked list.
*
* @author Samuel A. Rebelsky
* @version 1.0 of March 1999
*/
public class LinkedOrderedList
implements OrderedList
{
/** The elements of the list, starting at the front */
protected Node front;
/** The last element of the list. */
protected Node back;
/** The current element of the list. */
protected Node current;
...
} // LinkedOrderedList
LinkedOrderedList class, we might write
our insert at head function as follows
/**
* Insert a non-null object at the head of the list.
* pre: The element is not null.
* pre: The current list has been initialized
* post: The head of the list is now that element.
* post: The tail of the list is the previous list.
*/
public void insertAtHead(Object element) {
// Create a node to hold the new element.
Node new_head = new Node(element,null);
// Is the list currently empty? If so, just insert the element
// at front and back, and we're done.
if (front == null) {
front = new_head;
back = new_head;
current = new_head;
}
// Otherwise, update the next reference of the new head
// and replace the front of the list
else {
new_head.setNext(front);
front = new_head;
// We don't need to modify the back of the list.
// Should we modify current?
}
} // insertAtHead(Object)
back field.
Node-based version (in which we
do have a back.
/**
* Append an element to the end of the list.
* pre: The element is not null.
* pre: The list has been appropriately initialized and maintained.
* post: The list now contains the element at the end.
*/
public void appendToEnd(Object element) {
// Create the node for the new element
Node new_end = new Node(element,null);
// Special case: the list is empty
if (front == null) {
front = new_end;
back = new_end;
current = new_end;
}
// Normal case: we just need to update the end
else {
back.setNext(new_end);
back = new_end;
}
} // appendToEnd(Object)
back field.
How to do so is left as an exercise for the reader.
History
Back to Linked Lists. On to Object-Oriented Design (1).
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Outlines/outline.29.html
Source text last modified Mon Mar 15 15:01:55 1999.
This page generated on Fri Mar 19 08:37:04 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu