List code from Lab 5/2/01
/**
* Driver class for linked list explorations.
*
* @author (your name)
* @version (a version number or a date)
*/
import javax.swing.*;
public class Driver
{
public static void main(String [] args) {
JTextArea t = new JTextArea(10, 20);
JScrollPane sp = new JScrollPane(t);
ListReferenceBased list = new ListReferenceBased();
list.add(1, "Peter");
list.add(2, "Bob");
list.add(3, "Sally");
list.remove(2);
list.add(2, "Robert");
list.add(4, "Diane");
t.append( list.toString() );
//JOptionPane.showMessageDialog(null, sp);
showList(list);
System.exit(0);
}
public static String stuff() {
StringBuffer s = new StringBuffer();
for (int i = 1; i <= 30; i++) {
s.append("line " + i + "\n");
}
return s.toString();
}
public static void showList(Object l) {
JOptionPane.showMessageDialog(null, l.toString());
}
}
// ****************************************************
// Reference-based implementation of ADT list.
//
// This class has been modified from the author's orginal
// code to include some toString explorations and to add
// a tail reference. (PC 5/2/01)
//
// ****************************************************
public class ListReferenceBased implements ListInterface {
// reference to linked list of items
private Node head;
private Node tail;
private int numItems; // number of items in list
public ListReferenceBased() {
numItems = 0;
head = null;
tail = null;
} // end default constructor
public boolean isEmpty() {
return numItems == 0;
} // end isEmpty
public int size() {
return numItems;
} // end size
private Node find(int index) {
// --------------------------------------------------
// Locates a specified node in a linked list.
// Precondition: index is the number of the desired
// node. Assumes that 1 <= index <= numItems+1
// Postcondition: Returns a reference to the desired
// node.
// --------------------------------------------------
Node curr = head;
for (int skip = 1; skip < index; skip++) {
//System.out.println("find:" + skip);
curr = curr.getNext();
} // end for
return curr;
} // end find
public Object get(int index)
throws ListIndexOutOfBoundsException {
if (index >= 1 && index <= numItems) {
// get reference to node, then data in node
Node curr = find(index);
Object dataItem = curr.getItem();
return dataItem;
}
else {
throw new ListIndexOutOfBoundsException(
"List index out of bounds exception on get");
} // end if
} // end get
public void add(int index, Object item)
throws ListIndexOutOfBoundsException {
if (index >= 1 && index <= numItems+1) {
if (index == 1) {
// insert the new node containing item at
// beginning of list
Node newNode = new Node(item, head);
if ( isEmpty() ) {
tail = newNode;
}
head = newNode;
}
else if (index == numItems + 1) {
Node newNode = new Node(item, null);
tail.setNext( newNode );
tail = newNode;
}
else {
Node prev = find(index-1);
// insert the new node containing item after
// the node that prev references
Node newNode = new Node(item, prev.getNext());
prev.setNext(newNode);
} // end if
numItems++;
}
else {
throw new ListIndexOutOfBoundsException(
"List index out of bounds exception on add");
} // end if
} // end add
public void remove(int index)
throws ListIndexOutOfBoundsException {
if (index >= 1 && index <= numItems) {
if (index == 1) {
// delete the first node from the list
head = head.getNext();
if (head == null) {
tail = null;
}
}
else {
Node prev = find(index-1);
// delete the node after the node that prev
// references, save reference to node
Node curr = prev.getNext();
prev.setNext(curr.getNext());
if (prev.getNext() == null) {
tail = prev;
}
} // end if
numItems--;
} // end if
else {
throw new ListIndexOutOfBoundsException(
"List index out of bounds exception on remove");
} // end if
} // end remove
public void removeAll() {
// setting head to null causes list to be
// unreachable and thus marked for garbage
// collection
head = null;
tail = null;
numItems = 0;
} // end removeAll
public String toString() {
StringBuffer sb = new StringBuffer();
toStringHelper(head, sb);
return sb.toString();
}
public StringBuffer toStringHelper(Node n, StringBuffer s) {
if (n != null) {
toStringHelper( n.getNext(), s );
s.append( n.getItem().toString() );
if (n != head) {
s.append(", ");
}
}
return s;
}
public String toString2() {
StringBuffer sb = new StringBuffer();
if ( !isEmpty() ) {
int i;
for (i = 1; i < size(); i++) {
sb.append( get(i).toString() );
sb.append( ", " );
}
sb.append( get(i).toString() );
}
return sb.toString();
}
} // end ListReferenceBased