Question: IndexedListLinkedList.java: ublic class IndexedListLinkedList implements IndexedList { private LinearNode head; // reference to first node private LinearNode tail; // reference to last node private int

 IndexedListLinkedList.java: ublic class IndexedListLinkedList implements IndexedList { private LinearNode head; //

IndexedListLinkedList.java:
ublic class IndexedListLinkedList implements IndexedList { private LinearNode  head; // reference to first node private LinearNode  tail; // reference to last node private int count; // number of nodes in the list public IndexedListLinkedList ( ) // POST: empty list { head = tail = null; count = 0; } public void addFirst (T element) // POST: element added to front of list { LinearNode newNode = new LinearNode(element); if (isEmpty()) head = tail = newNode; else { newNode.setNext(head); head = newNode; } count++; } public void addLast (T element) // POST: element added to rear of list { LinearNode newNode = new LinearNode(element); if (isEmpty()) head = tail = newNode; else { tail.setNext(newNode); tail = newNode; } count++; } public void add (T element, int position) throws InvalidPositionException { // PRE: 0 = count) // POST: element added at position throw new InvalidPositionException (); if (position == 0) addFirst(element); else if (position == count) addLast(element); else { LinearNode current = head; for (int i = 1; i  newNode = new LinearNode (element); newNode.setNext(current.getNext()); current.setNext(newNode); count++; } } public T removeFirst( ) throws EmptyCollectionException // PRE: list is not empty { // POST: remove and return first element if (isEmpty()) throw new EmptyCollectionException(); LinearNode temp = head; head = head.getNext(); count--; if (isEmpty()) tail = null; T element = temp.getElement(); return element; } public T removeLast( ) throws EmptyCollectionException // PRE: list is not empty { // POST: remove and return last element if (isEmpty( )) throw new EmptyCollectionException( ); LinearNode temp = tail; if (count == 1) head = tail = null; else { LinearNode current = head; for (int k = 0; k= count) throw new InvalidPositionException( ); if (position == 0) return removeFirst( ); else if (position == count-1) return removeLast( ); else { LinearNode previous = head; for (int k = 1; k  current = previous.getNext( ); previous.setNext(current.getNext( )); count--; return current.getElement( ); } } public boolean isEmpty ( ) // POST: return true if list is empty, else false { return count == 0; } public int size( ) // POST: return number of elements in list { return count; } public void clear ( ) // POST: remove all nodes { head = tail = null; count = 0; } public String toString ( ) // POST: return a string representation of list elements { String result = "["; LinearNode current = head; while (current != null) { result = result + " " + current.getElement(); current = current.getNext(); } result = result + " ]"; return result; } // complete the remaining methods public T remove (T element) // POST: remove and return specified element { return null; // return null if element is not found } public T first ( ) throws EmptyCollectionException // PRE: list is not empty { return null; // POST: return first element } public T last ( ) throws EmptyCollectionException // PRE: list is not empty { return null; // POST: return last element } public boolean contains (T target) // POST: return true if list contains target, else false { return true; } public int indexOf (T element ) // POST: return position of element, else -1 { return 0; } public T get (int position) throws InvalidPositionException // PRE: 0  
TestIndexedListLinkedList.Java: (test Class) 
 import java.io.*; import java.util.Scanner; public class TestIndexedListLinkedList { public static void main(String[] args) { Scanner scan = new Scanner (System.in); IndexedListLinkedList list = new IndexedListLinkedList ( ); list.addLast("a"); list.addLast("b"); list.addLast("*"); list.addLast("c"); System.out.println("Tester by Your Name"); System.out.println("Original list: " + list.toString()); // test remove method list.remove("*"); // element * is found and removed list.remove("d"); // element d is not found and nothing done System.out.println("List after remove: " + list.toString()); // test first and last System.out.println("First element: " + list.first()); System.out.println("Last element: " + list.last()); // test contains if (list.contains("b")) System.out.println("List contains b"); else System.out.println("List does not contain b"); if (list.contains("d")) System.out.println("List contains d"); else System.out.println("List does not contain d"); // test indexOf System.out.println("Index of b is: " + list.indexOf("b")); System.out.println("Index of d is: " + list.indexOf("d")); // test get System.out.println("String at position 1 is: " + list.get(1)); // test set list.set(1, "*"); System.out.println("List after set position 1 to *: " + list.toString()); // test invalid position try { System.out.println("Element at position 1000 is: " + list.get(1000)); } catch (InvalidPositionException e) { System.out.println(1000 + " " + e.getMessage()); } finally { System.out.println("Test program is ending."); } scan.close(); } }

courses.txt:

COS103 COS120 COS125 COS213 COS220 COS221 COS225 COS226 COS235 COS250 COS301 COS312 COS331 COS350 COS397 COS412 COS420 COS430 COS440 COS451 COS460 COS470 COS480 COS490 COS497

students.txt:

Adams COS103 COS213 Benson COS213 COS220 Brown COS103 COS213 Cheng COS301 COS350 COS451 Davis COS440 COS451 COS480 Eghert COS125 COS220 French COS225 COS235 COS312 Georges COS235 COS250 COS312 Heinrich COS301 COS331 COS350 Jackson COS225 COS312 Keller COS235 COS221 COS312 Lee COS125 COS220 Manner COS301 COS331 COS350 COS397 Nelsom COS220 COS225 COS312 Petty COS301 COS331 COS451 Rumer COS397 COS440 COS490 Stein COS301 COS331 COS480 Thompson COS125 COS220 Venner COS221 Washington COS490 COS497 Zaghert COS103

LinearNode.Java:

public class LinearNode { private T element; // element private LinearNode next; // reference to next node or null public LinearNode () // POST: empty node { element = null; next = null; } public LinearNode (T elem) // POST: node element set { element = elem; next = null; } // accessors and modifiers public LinearNode getNext() { return next; } public void setNext (LinearNode node) { next = node; } public T getElement ( ) { return element; } public void setElement (T elem) { element = elem; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!