[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Back to Lists, Concluded. On to Designing an Othello game.
Held Tuesday, March 16
Summary
Contents
Handouts
Notes
Node class).
/**
* Delete the first element in the list, returning that element.
* pre: the list is initialized and nonempty.
* post: the list is one element smaller and no longer
* contains the previous head element.
* post: returns the deleted item.
*/
public Object deleteHead()
throws Exception
{
Node deleted = front;
// Update the front
front = front.nextNode();
// Update current, if necessary. == is the appropriate comparison
// operation since we want to make sure that we're referring to
// the same node, and not just two nodes with the same contents.
if (current == deleted) {
current = front;
}
// Return the front element
return deleted.contents();
} // deleteHead()
Node class, it
would also be reasonable to directly access Node's fields,
since these are related classes, and would be in the same package.
/**
* Delete the last element in the list, returning that element.
* pre: the list is nonempty
* post: the list is one element smaller and no longer
* contains the previous final element
* post: if the current element was the final element, the
* current element is now the first element.
*/
public Object deleteLast() throws ListException {
// The element we're about to delete
Node deleted;
// Special case: the list has one element
if (front.next == null) {
deleted = front;
front = null;
current = null;
back = null;
return deleted.contents();
} // only one element
// Standard case: the list has at least two elements
else {
// The element we're currently looking at
Object pointer = front;
// The element we're about to delete
Object
// Keep going until we reach the last node
while (pointer.nextNode().nextNode() != null) {
pointer = pointer.nextNode();
} // while
// We're now one step back from the end, so delete
// the next element.
deleted = pointer.next;
pointer.next = null;
// Update the current element if necessary.
if (current == deleted) {
current = front;
}
// Return the element
return deleted.contents();
} // List of length two or more
} // deleteLast
Node-based deletion.
/**
* Delete all copies of an element from a sequence of Nodes.
*/
public Node deleteCopies(Node list, Object element) {
// Base case: empty list
if (list == null) {
return null;
}
// The head of the list contains the element. Delete and
// recurse
else if (list.contents().equals(element)) {
return deleteCopies(list.tail(), element);
}
// The head of the list doesn't contain the element. Work on
// the tail.
else {
list.setNext(deleteCopies(list.tail(), element));
return list;
}
} // deleteCopies
/**
* Delete all copies of an element from a list.
* See the exam for comments.
*/
public void deleteCopies(element) {
front = deleteCopies(front,element);
// Fix current element if necessary
if (current.contents().equals(element)) current = front;
// Recompute the last element if necessary
if (back.contents().equals(element)) recomputeBack();
} // deleteCopies
reset makes current equal to
front.
nextElement notes the current element, advances
current, and then returns the current element.
hasMoreElements makes sure that
current is not null.
History
Back to Lists, Concluded. On to Designing an Othello game.
[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.30.html
Source text last modified Tue Mar 16 10:50:38 1999.
This page generated on Tue Mar 16 10:52:49 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu