Attached is theLinkedListclass from this chapter and theListIteratorclass. It has the reverse method, which I demonstrated on the Example video. Your assignment is to do the following:
- in addition to thefirstdata member, add alastdata member, which will always point to the last node in the list (or null if the list is empty).
- add a "size" method to the LinkedList class that returns the number of nodes in the list
- add an "addLast" method to the LinkedList class
- add a "removeLast" method to the LinkedList class
- add a "getLast" method to the LinkedList class
- add a "contains" method to the LinkedList class that returns true or false whether its parameter value is on the list
NOTE: NoSuchElementException should be thrown where appropriate
import java.util.NoSuchElementException; /** A program that demonstrates the LinkedList class */ public class ListDemo { public static void printList(LinkedList ll) { ListIterator iterator = ll.listIterator(); System.out.print("Size of list: " + ll.size() + "; list is "); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } if (ll.size() > 0) System.out.print("FIRST is " + ll.getFirst() + " "); if (ll.size() > 0) System.out.print("LAST is " + ll.getLast()); System.out.println(); } public static void main(String[] args) { LinkedList staff = new LinkedList(); printList(staff); staff.addFirst("Tom"); staff.addFirst("Romeo"); staff.addFirst("Harry"); printList(staff); try { staff.removeFirst(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeFirst(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeFirst(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeFirst(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.addLast("Roger"); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeFirst(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { System.out.println(staff.getLast()); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.addLast("Jane"); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.addLast("Judy"); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeLast(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeLast(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.addLast("Ken"); staff.addFirst("Debbie"); staff.addLast("Barbie"); staff.addFirst("Jack"); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { System.out.println(staff.contains("Mark")); System.out.println(staff.contains("Jack")); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeLast(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeLast(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeLast(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeLast(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { staff.removeLast(); printList(staff); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } try { System.out.println(staff.contains("Mark")); System.out.println(staff.contains("Jack")); } catch (NoSuchElementException ex) { System.out.println ("No such element"); } } } /** A list iterator allows access of a position in a linked list. This interface contains a subset of the methods of the standard java.util.ListIterator interface. The methods for backward traversal are not included. */ public interface ListIterator { /** Moves the iterator past the next element. @return the traversed element */ Object next(); /** Tests if there is an element after the iterator position. @return true if there is an element after the iterator position */ boolean hasNext(); /** Adds an element before the iterator position and moves the iterator past the inserted element. @param element the element to add */ void add(Object element); /** Removes the last traversed element. This method may only be called after a call to the next() method. */ void remove(); /** Sets the last traversed element to a different value. @param element the element to set */ void set(Object element); }