Fall Semester, 1997
Doc 15, Comments on Assignment 1, part 2

To Lecture Notes Index

References

Various student papers

`Doc 15, Comments on Assignment 1, part 2 Slide # 1`

One(?) Abstraction Solution

```class Node
{
int element;
Node next;
}

class Test
{
public  static  void  main( String  args[] )
{
prompt user for input
create nodes
count sum
compute average
}
}
```

`Doc 15, Comments on Assignment 1, part 2 Slide # 2`

The Two in One Solution

```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
}
```

`Doc 15, Comments on Assignment 1, part 2 Slide # 3`

What to do with Node?

```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;
}
}
Doc 15, Comments on Assignment 1, part 2 Slide # 4
```
A little Intelligence for Node
```class Node
{
int data;
Node next = null;

public Node( int initialData, Node nextInList )
{
data = initialData;
next = nextInList;
}
}

{
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++;
}
}
Doc 15, Comments on Assignment 1, part 2 Slide # 5
```

How to Go through each element of the list

```class LinkedListEnumeration implements Enumeration
{
private Node nextNode;

{
nextNode = aList.front;
}

public boolean hasMoreElements()
{
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

`Doc 15, Comments on Assignment 1, part 2 Slide # 6`
```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()
{
}

public int size()
{
return nodeCount;
}

Doc 15, Comments on Assignment 1, part 2 Slide # 7
```
```
public String toString()
{
StringBuffer stringifiedList = new StringBuffer( size() * 2);
Enumeration list = elements();

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[] )
{
sample.prepend( 4 );
sample.prepend( 3 );
sample.prepend( 2 );
sample.prepend( 1 );
System.out.println( sample );
}
}
Doc 15, Comments on Assignment 1, part 2 Slide # 8
```

toString Issues

• * 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.

`Doc 15, Comments on Assignment 1, part 2 Slide # 9`

Giving the Node Some More Intelligence

```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);
}

Doc 15, Comments on Assignment 1, part 2 Slide # 10
```
//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;
}
}
Doc 15, Comments on Assignment 1, part 2 Slide # 11
```

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?

visitors since 07-Oct-97