[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [Tutorial] [API]
Back to Adding Elements to Linked Lists. On to Introduction to Linear Structures.
Held Friday, April 7, 2000
Overview
Today, we conclude our discussion of linked lists by considering how to delete elements from linked lists.
Notes
Contents
Summary
/**
* An implementation of simple list usings linked
* nodes. Each node contains a link to the next
* element of the list. This version still lacks
* delete methods.
*
* @author Samuel A. Rebelsky
* @author The Students of CSC152 2000S
* @version 0.1 of April 2000
*/
public class SimpleLinkedList
implements SimpleList
{
// +--------+-----------------------------------------
// | Fields |
// +--------+
/**
* The first element of the list. All other elements are
* reachable from this element. This element is set to null
* if the list is empty.
*/
protected ConsCell front;
/**
* The last element of the list. Useful when we want to
* quickly add to the end of the list.
*/
protected ConsCell back;
/**
* The current element of the list. Used for iteration.
*/
protected ConsCell current;
// +--------------+-----------------------------------
// | Constructors |
// +--------------+
/**
* Create a new empty list.
*/
public SimpleLinkedList() {
front = null;
back = null;
current = null;
}
// +----------------------+---------------------------
// | Iteration Operations |
// +----------------------+
/**
* Get the current element of the list.
* Pre: There is a current element.
* Post: Returns the current element.
*/
public Object getCurrent() {
return current.getContents();
} // getCurrent()
/**
* Advances to the next element.
* Pre: The list is nonempty and there is a current element.
* Post: Advanced to next element.
*/
public void advanceCurrent() {
current = current.cdr();
} // advanceCurrent()
/**
* Make the first element of the list the current element.
* Pre: The list is nonempty.
* Post: The first element is the current element.
*/
public void reset() {
current = front;
} // reset()
// +-----------+----------=---------------------------
// | Modifiers |
// +-----------+
/**
* Add an element to the end of the list.
* Pre: (none)
* Post: The new element is the last element of the list.
* The other elements are as before.
* The old last element is the penultimate element.
*/
public void addToEnd(Object something) {
// Create a new cell to hold the thing.
ConsCell newCell = new ConsCell(something, null);
// Is the list empty? If so, build a one-element list
if (this.front == null) {
this.front = newCell;
this.back = newCell;
this.current = newCell;
} // if the front is empty
// Put it after the last element
back.setNext(newCell);
// Make it the new last element.
back = newCell;
} // addToEnd(Object)
/**
* Add an element before the first element of the list.
* Pre: (none)
* Post: The new element is the first element.
* The next element of the new element is the old first.
* All other elements are in the same order.
*/
public void addToFront(Object something) {
// Create a new cell to hold the thing.
ConsCell newCell = new ConsCell(something, front);
// Update our notion of front
front = newCell;
// Special case: The list was empty
if (back == null) {
back = newCell;
current = newCell;
}
} // addToFront(Object)
} // class SimpleLinkedList
Tuesday, 18 January 2000
Friday, 7 April 2000
Back to Adding Elements to Linked Lists. On to Introduction to Linear Structures.
[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [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/2000S/Outlines/outline.36.html
Source text last modified Fri Apr 7 09:53:22 2000.
This page generated on Fri Apr 7 09:55:14 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu