CS 535: Object-Oriented Programming & Design |
---|
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; }
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; } }
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 ); } }
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; }
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 ); } }
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); }
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; } }