package hw4; import java.util.NoSuchElementException; /** *
A simple implementation of singly linked lists.
* *Lists are created empty, for example: *
* LinkedList list = new LinkedList(); ** after which items can be inserted at the front of the list by *
* list.addFirst(item) ** where item is any Object. * *
Items can be removed from the front of the list by
** list.removeFirst(); ** which removes the first item from the list. * * @see Iterator * * @author Peter Williams */ public class LinkedList { private ListNode head; // the front of the list private int size; static class ListNode { Object data; ListNode next; ListNode(Object data, ListNode next) { this.data = data; this.next = next; } } /** * Constructs an empty list. */ public LinkedList() { head = null; size = 0; } /** * Returns the length of the list * @return the length of the list */ public int size() { return size; } /** * Inserts an item at the front of the list. * @param item the Object to be inserted. */ public void addFirst(Object item) { head = new ListNode(item, head); ++size; } /** * Returns the first item in the list. * @return the item at the front of the list. * @exception NoSuchElementException if the list is empty. */ public Object getFirst() { if (head != null) { return head.data; } else { throw new NoSuchElementException(); } } /** * Removes the first item from the list. * @exception NoSuchElementException if the list is empty. */ public void removeFirst() { if (head != null) { head = head.next; --size; } else { throw new NoSuchElementException(); } } /** * Tests whether an item is present in the list. * @param item the object to be searched for. * @return true if the item is present, * false otherwise */ public boolean contains(Object item) { ListNode current = head; while (current != null) { if (item.equals(current.data)) { return true; } else { current = current.next; } } return false; } /** * Reverses the list. */ public void reverse() { ListNode current = head; head = null; while (current != null) { ListNode save = current; current = current.next; save.next = head; head = save; } } /** * Implements the equals method. * @param other the second linked list. * @return true if the lists are equal, false otherwise. */ public boolean equals(Object other) { if (other == null || !(other instanceof LinkedList) || this.size != ((LinkedList) other).size) { return false; } ListNode p = this.head; ListNode q = ((LinkedList) other).head; while (p != null) { if (!p.data.equals(q.data)) { return false; } p = p.next; q = q.next; } return true; } /** * Converts the list to a string. * @return the string representation. */ public String toString() { String string = ""; string += "["; if (head != null) { string += head.data; ListNode current = head.next; while (current != null) { string += ", " + current.data; current = current.next; } } string += "]"; return string; } }