Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implementation of a node of a singly linked list.
- *
- * Adapted from the College Board's AP Computer Science AB:
- * Implementation Classes and Interfaces.
- */
- public class ListNode
- {
- private Object value;
- private ListNode next;
- /**
- * Constructs a new element with object initValue,
- * followed by next element
- *
- * @param initValue New element object
- * @param initNext Reference to next element
- */
- public ListNode(Object initValue, ListNode initNext)
- {
- value = initValue;
- next = initNext;
- }
- /**
- * Constructs a new tail of a list with object initValue
- *
- * @param initValue New element object
- */
- public ListNode(Object initValue)
- {
- this(initValue, null);
- }
- /**
- * Sets the value attribute of the ListNode object
- *
- * @param theNewValue value attribute of the ListNode object
- */
- public void setValue(Object theNewValue)
- {
- value = theNewValue;
- }
- /**
- * Sets reference to new next value
- *
- * @param theNewNext The new next value
- */
- public void setNext(ListNode theNewNext)
- {
- next = theNewNext;
- }
- /**
- * Returns value associated with this element
- *
- * @return The value associated with this element
- */
- public Object getValue()
- {
- return value;
- }
- /**
- * Returns reference to next value in list
- *
- * @return The next value in the list
- */
- public ListNode getNext()
- {
- return next;
- }
- }
- /**
- * Demostrates the use of the SinglyLinkedList class.
- *
- * @author G. Peck
- * @created May 12, 2002
- */
- public class ListDemo
- {
- SinglyLinkedList myList;
- public ListDemo()
- {
- myList = new SinglyLinkedList();
- }
- /**
- * Creates a SinglyLinkedList of 5 Integer nodes
- */
- public void createList()
- {
- for (int k = 1; k <= 20; k++)
- {
- myList.addFirst(new Integer(k));
- }
- }
- /**
- * Display the first element of the list
- */
- public void displayFirst()
- {
- System.out.println("First Element: " + myList.getFirst());
- }
- public void displayLast()
- {
- System.out.println("Last Element: " + myList.getLast());
- }
- /**
- * Print the contents of myList
- */
- public void print()
- {
- myList.printList();
- System.out.println();
- }
- /**
- * Demostrates the use of the SinglyLinkedList class.
- * Creates and prints a list of 5 consecutive Integer ojects.
- *
- * @param args The command line arguments (not used)
- */
- public static void main(String[] args)
- {
- ListDemo sList = new ListDemo();
- sList.createList();
- sList.displayFirst();
- sList.print();
- sList.displayLast();
- }
- }
- import java.util.*;
- /**
- * Implementation of lists, using singly linked elements.
- *
- * @author G. Peck
- * @created April 27, 2002
- */
- public class SinglyLinkedList
- {
- private ListNode first; // first element
- private ListNode last;
- /**
- * Constructor for the SinglyLinkedList object
- * Generates an empty list.
- */
- public SinglyLinkedList()
- {
- first = null;
- }
- /**
- * Returns the first element in this list.
- *
- * @return the first element in the linked list.
- */
- public Object getFirst()
- {
- if (first == null)
- {
- throw new NoSuchElementException();
- }
- else
- return first.getValue();
- }
- /**
- * Inserts the given element at the beginning of this list.
- *
- * @param value the element to be inserted at the beginning of this list.
- */
- public void addFirst(Object value)
- {
- // note the order that things happen:
- // head is parameter, then assigned
- first = new ListNode(value, first);
- }
- public void addLast(Object value)
- {
- if(first == null)
- {
- first = new ListNode(value, first.getNext());
- last = new ListNode(value, first.getNext());
- }
- else
- last = new ListNode(value, last.getNext());
- }
- public Object getLast()
- {
- return last.getValue();
- }
- /**
- * Print the contents of the entire linked list
- */
- public void printList()
- {
- ListNode temp = first;// start from the first node
- while (temp != null)
- {
- System.out.print(temp.getValue() + " ");
- temp = temp.getNext();// go to next node
- }
- }
- /**
- * Returns a string representation of this list. The string
- * representation consists of the list's elements in order,
- * enclosed in square brackets ("[]"). Adjacent elements are
- * separated by the characters ", " (comma and space).
- *
- * @return string representation of this list
- */
- public String toString()
- {
- String s = "[";
- ListNode temp = first; // start from the first node
- while (temp != null)
- {
- s += temp.getValue(); // append the data
- temp = temp.getNext(); // go to next node
- if (temp != null)
- s += ", ";
- }
- s += "]";
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment