Question: In the Assignment, you are given three files Assignment10.java, LinkedList.java, ListIterator.java. You will need to add additional methods in the LinkedList class in the LinkedList.java
In the Assignment, you are given three files Assignment10.java, LinkedList.java, ListIterator.java. You will need to add additional methods in the LinkedList class in the LinkedList.java file. The LinkedList will be tested using strings only. Specifically, the following methods must be implemented in the LinkedList class: (You should utilize listIterator() method already defined in the LinkedList class to obtain its LinkedListIterator object, and use the methods in the LinkedListIterator class to traverse from the first element to the last element of the linked list to define the following methods.)
public int size( ) : The size method returns the number of strings that the linked list contains at the time when this method is called.
public int searchElement(Object element) : The searchElement method checks at which index the parameter element (string) is located, and returns its index. If the linked list does not contain the parameter element, then it returns -1.
public Object getElement(int index) : The getElement method returns an element at the given index passed as its parameter. If the parameter index is larger or smaller than the existing indices, it should throw an IndexOutOfBoundsException
public void setElement (int index, Object element) : The setElement method set/replace the original element at parameter index with the parameter element, all other elements remain unchanged. If the parameter index is larger or smaller than the existing indices, it should throw an object of the IndexOutOfBoundsException class.
public Object insertElement(int index, Object element ) : The insertElement method inserts the parameter object at the parameter index. If the index is out of bounds, throws an IndexOutOfBoundException. Note: the element can be inserted at the end of the linked list.
public Object removeElement(int index) : The removeElement method removes and returns element at parameter index and throw an IndexOutOfBoundException if the index is out of bound.
public int countHowMany(Object searchedObject) : The countHowMany method returns the number of occurences of the parameter object in the LinkedList. It returns 0 if the parameter object does not exist in the linked list.
public void removeDuplicate(Object removedObject) : The removeDuplicate method removes all occurences of the parameter object from the LinkedList.
public void appendAtEnd(Object element, int howManyTimes) : The appendAtEnd method appends the parameter object the parameter number of times at the end of the linked list. For example, a call of list1.appendAtEnd("Dog", 3) will append string "Dog" three times at the end of the linked list.
public void appendAfter(Object element1, Object element2) : The appendAfter method appends the second parameter object, i.e. element2 right after the first occurence of first parameter object, i.e. element1. If element1 is not inside the linked list, then element2 will be appended at the front/head of the linked list.
public void reverseFirstFew(int howMany) : The reverseFistFew method reverses the first parameter number of elements inside the linked list. For example, if the original linked list is { A B C D E F G }, a call of list.reverseFirstFew(3) will change the linked list to { C B A D E F G }. Note: (1) You need to consider the boundary value, i.e.cases where howMany <= 0 or howMany > list.size( ). (2): list.reverseFirstFew(list.size( ) ) should be able to reverse the whole linked list.
import java.util.NoSuchElementException; public class LinkedList { //nested class to represent a node private class Node { public Object data; public Node next; } //only instance variable that points to the first node. private Node first; // Constructs an empty linked list. public LinkedList() { first = null; } // Returns the first element in the linked list. public Object getFirst() { if (first == null) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else return first.data; } // Removes the first element in the linked list. public Object removeFirst() { if (first == null) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else { Object element = first.data; first = first.next; //change the reference since it's removed. return element; } } // Adds an element to the front of the linked list. public void addFirst(Object element) { //create a new node Node newNode = new Node(); newNode.data = element; newNode.next = first; //change the first reference to the new node. first = newNode; } // Returns an iterator for iterating through this list. public ListIterator listIterator() { return new LinkedListIterator(); } /********************************************************* methods here *********************************************************/ //nested class to define its iterator private class LinkedListIterator implements ListIterator { private Node position; //current position private Node previous; //it is used for remove() method // Constructs an iterator that points to the front // of the linked list. public LinkedListIterator() { position = null; previous = null; } // Tests if there is an element after the iterator position. public boolean hasNext() { if (position == null) //not traversed yet { if (first != null) return true; else return false; } else { if (position.next != null) return true; else return false; } } // Moves the iterator past the next element, and returns // the traversed element's data. public Object next() { if (!hasNext()) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else { previous = position; // Remember for remove if (position == null) position = first; else position = position.next; return position.data; } } // Adds an element before the iterator position // and moves the iterator past the inserted element. public void add(Object element) { if (position == null) //never traversed yet { addFirst(element); position = first; } else { //making a new node to add Node newNode = new Node(); newNode.data = element; newNode.next = position.next; //change the link to insert the new node position.next = newNode; //move the position forward to the new node position = newNode; } //this means that we cannot call remove() right after add() previous = position; } // Removes the last traversed element. This method may // only be called after a call to the next() method. public void remove() { if (previous == position) //not after next() is called { IllegalStateException ex = new IllegalStateException(); throw ex; } else { if (position == first) { removeFirst(); } else { previous.next = position.next; //removing } //stepping back //this also means that remove() cannot be called twice in a row. position = previous; } } // Sets the last traversed element to a different value. public void set(Object element) { if (position == null) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else position.data = element; } } //end of LinkedListIterator class } //end of LinkedList class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
