Question: The code for this problem is in Java. (Had to delete a few methods since the question is too long) I need help with: public
The code for this problem is in Java. (Had to delete a few methods since the question is too long)
I need help with:
public void addLast(T element)
public void addAfter(T existing, T element) throws ElementNotFoundException, EmptyCollectionException
public T remove(T element) throws EmptyCollectionException, ElementNotFoundException
public T removeLast() throws EmptyCollectionException
public boolean contains(T element) throws EmptyCollectionException
==========CircularDoublyLinkedList==========
package DataStructures;
import ADTs.ListADT; import Exceptions.ElementNotFoundException; import Exceptions.EmptyCollectionException; import Exceptions.InvalidArgumentException; /** * * @author ITSC 2214 Q. * @param
/** * Constructor. */ public CircularDoublyLinkedList() { first = null; numNodes = 0; } /** * Retrieve the first element in the circular list, * which is stored in the current first node * @return first element */ @Override public T first() throws EmptyCollectionException{ if (first == null) throw new EmptyCollectionException("CircularDoublyLinkedList");
return first.getElement(); } /** * Print all elements in the list. */ public void printList() { // Is list empty? if(first == null) return;
// start at the head and print everyone boolean startFlag = true; for(DoublyLinkedNode tmp=first; (tmp != first) || startFlag; tmp=tmp.getNext()) { System.out.println(tmp.getElement().toString()); startFlag = false; } } /** * Add a new element in the end of the circular list, * which next reference points to the current first node. * @param element */ @Override public void addLast(T element) { //TODO create the new node
//TODO adjust references for new node //TODO increase the numNodes variable
} /** * Add a new element in the beginning of the circular list, * which is represented as the current first node. */ @Override public void addFirst(T element) { // create the new node addLast(element); //TODO adjust the current first node reference
} /** * Insert a new node which holds the reference to the given element, * after the node which holds the given existing. * Namely insert the given element after the existing element in the list If the given existing is not found, throw an ElementNotFoundException.
Note: the CircularDoublyLinkedList class uses the data member variable first and numNodes to maintain the status of a CircularDoublyLinkedList instance. When the method addAfter is invoked, we need to make sure that the data member variable first, and numNodes are changed properly. Do we need to change the data member variable first? And when? * @param existing * @param element * @throws ElementNotFoundException * @throws EmptyCollectionException */ @Override public void addAfter(T existing, T element) throws ElementNotFoundException, EmptyCollectionException { DoublyLinkedNode
/** * Remove a node that holds the given element. * @param element * @return * @throws EmptyCollectionException * @throws ElementNotFoundException */ @Override public T remove(T element) throws EmptyCollectionException, ElementNotFoundException { DoublyLinkedNode
}
/** * Remove the first node in the list. * (prior to the current first node) * @return * @throws EmptyCollectionException */ @Override public T removeFirst() throws EmptyCollectionException { // case 1: empty list if(first == null) throw new EmptyCollectionException("CircularDoublyLinkedList"); DoublyLinkedNode
/** * Remove the last node in the list. * (prior to the current first node) * @return * @throws EmptyCollectionException */ @Override public T removeLast() throws EmptyCollectionException { // TODO remove last node and maintain all references // including the current first node reference. //case 1: empty list
// return a pointer to the removed node return tmp.getElement(); }
/** * Examine whether the list includes the given element. * @param element * @return * @throws EmptyCollectionException */ @Override public boolean contains(T element) throws EmptyCollectionException { //TODO refer to the printList method
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
