| CS 535: Object-Oriented Programming & Design |
|
|---|
Fall Semester, 1997
Doc 15, Comments on Assignment 1, part 2
To Lecture Notes Index
San Diego State University -- This page last updated 07-Oct-97
Contents of Doc 15, Comments on Assignment 1, part 2
- References
- Problem 3 - Linked List
- One(?) Abstraction Solution
- The Two in One Solution
- What to do with Node?
- How to Go through each element of the list
- toString Issues
- Giving the Node Some More Intelligence
- Issues and Comments
Various student papers
class Node
{
int element;
Node next;
}
class Test
{
public static void main( String args[] )
{
prompt user for input
create nodes
link nodes
count sum
compute average
}
}
class Node
{
int element;
Node next = null;
Node front;
static int nodeCount;
public void prepend( int data )
{
Node newElement = new Node();
newElement.element = data;
newElement.next = next;
front = this;
nodeCount++;
}
public int size()
{
return nodeCount;
}
//other methods not shown
}
class Node
{
int element;
Node next;
}
verses
class Node
{
private int data;
private Node next = null;
public void setNext( Node aNode )
{
aNode.next = next;
next = aNode;
}
public Node getNext()
{
return next;
}
public void setData( int newData )
{
data = newData;
}
public int getData()
{
return data;
}
}
A little Intelligence for Node
class Node
{
int data;
Node next = null;
public Node( int initialData, Node nextInList )
{
data = initialData;
next = nextInList;
}
}
public class LinkedList
{
final static Node END_OF_LIST = null;
private Node front = END_OF_LIST;
private int nodeCount = 0;
public void prepend( int data )
{
front = new Node( data, front );
nodeCount++;
}
}
class LinkedListEnumeration implements Enumeration
{
private Node nextNode;
public LinkedListEnumeration( LinkedList aList )
{
nextNode = aList.front;
}
public boolean hasMoreElements()
{
if (nextNode == LinkedList.END_OF_LIST )
return false;
else
return true;
}
public Object nextElement()
{
int data = nextNode.data;
nextNode = nextNode.next;
return new Integer( data );
}
}
Issues
Should enumeration return a node or the data in a node?
External to LinkedList do we want the node exposed?
Internal we may need the node not the data
Using the LinkedListEnumeration
public class LinkedList
{
final static Node END_OF_LIST = null;
Node front = END_OF_LIST;
private int nodeCount = 0;
public void prepend( int data )
{
front = new Node( data, front );
nodeCount++;
}
public Enumeration elements()
{
return new LinkedListEnumeration( this );
}
public int size()
{
return nodeCount;
}
//LinkedList Continued
public String toString()
{
StringBuffer stringifiedList = new StringBuffer( size() * 2);
Enumeration list = elements();
stringifiedList.append( "LinkedList(");
while ( list.hasMoreElements() )
{
stringifiedList.append( list.nextElement() );
stringifiedList.append( ", ");
}
// added one too many ", ", so remove last one
stringifiedList.setLength( stringifiedList.length() -2 );
stringifiedList.append( ")");
return stringifiedList.toString();
}
}
class Test
{
public static void main( String args[] )
{
LinkedList sample = new LinkedList();
sample.prepend( 4 );
sample.prepend( 3 );
sample.prepend( 2 );
sample.prepend( 1 );
System.out.println( sample );
}
}
- * toString method does not use links!
- Went every you need to traverse the list use the enumeration
- * I did not perform any IO in the toString method
- * If list is very long, toString() will consume lots of memory
- For now we can limit the number of items that toString returns
- Will see later (use stream/writer) how to deal with long lists.
class Node
{
protected int data;
protected Node next = null;
protected Node previous = null;
public Node( int initialData,
Node previousInList,
Node nextInList )
{
data = initialData;
next = nextInList;
previous = previousInList;
}
public void append( int data)
{
next = new Node( data, this, next);
}
public void append( Node newNode)
{
newNode.previous = this;
newNode.next = next;
next = newNode;
}
public void prepend( int data)
{
previous = new Node( data, previous, this);
}
//Node Continued
public void prepend( Node newNode)
{
newNode.previous = previous;
newNode.next = this;
previous = newNode;
}
public void remove()
{
previous.next = next;
next.previous = previous;
}
public int getData()
{
return data;
}
public void setData( int newData )
{
data = newData;
}
}
Now the LinkedList does not have to be full of links/unlinks operations
Do we need two append methods?
Do we need more constructors?
What is with this Node name? How about ListNode!
The Node class is responsible for handling all links with its neighbors
The LinkedList class deals with global issues, where do node go in the list
Enumeration deals with traversing the list
Can we make Node even smarter? Should we?