SDSU 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

  1. References
  2. Problem 3 - Linked List
    1. One(?) Abstraction Solution
    2. The Two in One Solution
    3. What to do with Node?
    4. How to Go through each element of the list
    5. toString Issues
    6. Giving the Node Some More Intelligence
    7. Issues and Comments

References


Various student papers



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

Problem 3 - Linked List

One(?) Abstraction Solution

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


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;
      }   
   }
   
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++;
      }
   }

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;
   
   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

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


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

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

toString Issues

Went every you need to traverse the list use the enumeration
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

Issues and Comments


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?

visitors since 07-Oct-97