Menu

Please Use Java Please Show Output Public Class Singlylinkedlist Implements Cloneable Ite Q43875124

Please Use Java and please show your outputtoo.Your Tasks Complete the following tasks. When you are finished you should be able to run the provided tester and get correct

public class SinglyLinkedList<E> implements Cloneable,Iterable<E> {
//—————- nested Node class —————-
/**
* Node of a singly linked list, which stores a reference toits
* element and to the subsequent node in the list (or null ifthis
* is the last node).
*/
private static class Node<E> {

/** The element stored at this node */
private E element; // reference to the element stored at thisnode

/** A reference to the subsequent node in the list */
private Node<E> next; // reference to the subsequent node inthe list

/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n reference to a node that should follow the newnode
*/
public Node(E e, Node<E> n) {
element = e;
next = n;
}

// Accessor methods
/**
* Returns the element stored at the node.
* @return the element stored at the node
*/
public E getElement() { return element; }

/**
* Returns the node that follows this one (or null if no suchnode).
* @return the following node
*/
public Node<E> getNext() { return next; }

// Modifier methods
/**
* Sets the node’s next reference to point to Node n.
* @param n the node that should follow this one
*/
public void setNext(Node<E> n) { next = n; }
} //———– end of nested Node class ———–

// instance variables of the SinglyLinkedList
/** The head node of the list */
private Node<E> head = null; // head node of the list (ornull if empty)

/** The last node of the list */
private Node<E> tail = null; // last node of the list (ornull if empty)

/** Number of nodes in the list */
private int size = 0; // number of nodes in the list

/** Constructs an initially empty list. */
public SinglyLinkedList() { } // constructs an initially emptylist

// access methods
/**
* Returns the number of elements in the linked list.
* @return number of elements in the linked list
*/
public int size() { return size; }

/**
* Tests whether the linked list is empty.
* @return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() { return size == 0; }

/**
* Returns (but does not remove) the first element of the list
* @return element at the front of the list (or null if empty)
*/
public E first() { // returns (but does not remove) the firstelement
if (isEmpty()) return null;
return head.getElement();
}

/**
* Returns (but does not remove) the last element of the list.
* @return element at the end of the list (or null if empty)
*/
public E last() { // returns (but does not remove) the lastelement
if (isEmpty()) return null;
return tail.getElement();
}
/**
* Returns (but does not remove) the penultimate node of thelist.
* @return node before the end of the list – exception if size <2 (IllegalStateException)
*/
  
private Node<E> penultimateNode( ) {
   //***** Complete this method ***** //
   return null;
}
/**
* Returns (but does not remove) the penultimate element of thelist.
* @return element before the end of the list – exception if size< 2 (IllegalStateException)
*/
public E penultimate() {
   return penultimateNode().getElement();
}
/**
* Returns (but does not remove) the middle element of thelist.
* @return element before the end of the list – exception if size =0 (IllegalStateException)
*/
public E getMiddle() {
   //***** Complete this method*****//
   return null;
}
// update methods
/**
* Adds an element to the front of the list.
* @param e the new element to add
*/
public void addFirst(E e) { // adds element e to the front of thelist
head = new Node<>(e, head); // create and link a newnode
if (size == 0)
tail = head; // special case: new node becomes tail also
size++;
}

/**
* Adds an element to the end of the list.
* @param e the new element to add
*/
public void addLast(E e) { // adds element e to the end of thelist
Node<E> newest = new Node<>(e, null); // node willeventually be the tail
if (isEmpty())
head = newest; // special case: previously empty list
else
tail.setNext(newest); // new node after existing tail
tail = newest; // new node becomes the tail
size++;
}
/**
* Adds an element to be the second of the list.
* @param e the new element to add
*/
public void addSecond(E e) {
   //***** Complete this method *****//
}
  
/**
* Adds an element at the ith position in the list.
* @param e the new element to add.
*/
public void add(E e, int i) {
   //***** Complete this method *****//
}

/**
* Removes and returns the first element of the list.
* @return the removed element (or null if empty)
*/
public E removeFirst() { // removes and returns the firstelement
if (isEmpty()) return null; // nothing to remove
E answer = head.getElement();
head = head.getNext(); // will become null if list had only onenode
size–;
if (size == 0)
tail = null; // special case as list is now empty
return answer;
}
/**
* Removes and returns the ith element of the list.
* @return the removed element exception i not in proper range
*/
public E remove(int i) {
   //***** Complete this method *****//
   return null;
}
  
/**
* Reverses the order of the nodes in the list.
*
*/
public void reverse() {
   //***** Complete this method *****//
}

@SuppressWarnings({“unchecked”})
public boolean equals(Object o) {
if (o == null) return false;
if (getClass() != o.getClass()) return false;
SinglyLinkedList other = (SinglyLinkedList) o; // usenonparameterized type
if (size != other.size) return false;
Node walkA = head; // traverse the primary list
Node walkB = other.head; // traverse the secondary list
while (walkA != null) {
if (!walkA.getElement().equals(walkB.getElement())) return false;//mismatch
walkA = walkA.getNext();
walkB = walkB.getNext();
}
return true; // if we reach this, everything matchedsuccessfully
}

@SuppressWarnings({“unchecked”})
public SinglyLinkedList<E> clone() throwsCloneNotSupportedException {
// always use inherited Object.clone() to create the initialcopy
SinglyLinkedList<E> other = (SinglyLinkedList<E>)super.clone(); // safe cast
if (size > 0) { // we need independent chain of nodes
other.head = new Node<>(head.getElement(), null);
Node<E> walk = head.getNext(); // walk through remainder oforiginal list
Node<E> otherTail = other.head; // remember most recentlycreated node
while (walk != null) { // make a new node storing sameelement
Node<E> newest = new Node<>(walk.getElement(),null);
otherTail.setNext(newest); // link previous node to this one
otherTail = newest;
walk = walk.getNext();
}
}
return other;
}

public int hashCode() {
int h = 0;
for (Node walk=head; walk != null; walk = walk.getNext()) {
h ^= walk.getElement().hashCode(); // bitwise exclusive-or withelement’s code
h = (h << 5) | (h >>> 27); // 5-bit cyclic shift ofcomposite code
}
return h;
}

/**
* Produces a string representation of the contents of thelist.
* This exists for debugging purposes only.
*/
public String toString() {
StringBuilder sb = new StringBuilder(“(“);
Node<E> walk = head;
while (walk != null) {
sb.append(walk.getElement());
if (walk != tail)
sb.append(“, “);
walk = walk.getNext();
}
sb.append(“)”);
return sb.toString();
}
   /**
   * Implements java.lang.Iterable<T>
   */
   public java.util.Iterator<E> iterator() {
       return new ForwardIterator();
   }
   private class ForwardIterator implementsjava.util.Iterator<E> {
       //***** Define appropriate instancevariables here *****//

       public boolean hasNext() {
           //***** Completethis method *****//
           returnfalse;
       }

       // Note: this method hasundefined behavior if hasNext() return false
       public E next() {
           //***** Completethis method *****//
           returnnull;
       }
      
   }
}

Your Output should look something like this.

***** BEGIN penultimate() Tests ***** Error triggered calling penultimate() on empty list: list must have 2 or more entries E

Empty list: () Size: 0 ***** BEGIN getMiddle() TEST ***** Error triggered calling getMiddle() on empty list list must have 1

Your Tasks Complete the following tasks. When you are finished you should be able to run the provided tester and get correct output for all tasks. a. Complete the penultimateNode () method. (See exercise R-3.6). b. Complete a reverse () method for the SinglyLinkedList class. Empty method has been provided for you. C. Add a addSecond (E e) method – If list is empty, throw exception. Parameter e will be the second element in the list after this operation has completed. This method should return void. d. Complete an add (E e, int i) method to the SinglyLinkedList class. What is the run time of this method? Throw an exception ifi is an invalid index (we covered an appropriate exception in the scoreboard example in class). (i must be >= 0 and <= size). After this operation, the parameter e will be stored in a node at the given index i. Method returns void. e. Add a remove (int i) method to the SinglyLinkedList class – What is the run time of this method? Throw an exception if į is an invalid index. This method should also return the Element that was removed. f. Complete a getMiddle () method. Throw an exception (InvalidState) if the list is empty. If there is only one element, return that element. If there is an even number of elements return the “left” element. g. Complete a ForwardIterator class. Look at the java.util. Iterator javadocs for information about how this function should operate. You only need to support hasNext () and next () methods. w w w m ***** BEGIN penultimate() Tests ***** Error triggered calling penultimate() on empty list: list must have 2 or more entries Error triggered calling penultimate() on list with 1 element: list must have 2 or more entries (1, 2) Penultimate: 1 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) Penultimate: 9 ***** BEGIN reverse() TEST ***** Reverse the list from previous test: (10, 9, 8, 7, 6, 5, 4, 3, 2, 1) Reverse empty list O Reverse list size 1 (1) Reverse list size 2 (2, 1) Reverse list size 3 (3, 2, 1) ***** BEGIN addSecond(E e) TEST ***** Error triggered calling addSecond(1) on list with elements: Can’t addSecond to an empty list Adding Second from 5 down to 2 on list (1). Expect: (1,2,3,4,5) List (1, 2, 3, 4, 5) Size: 5 ***** BEGIN add(E e, int i) TEST ***** Error triggered calling add(1,-1) on empty list Invalid index. Index must be >=0 and <=size. Error triggered calling add(1,1) on empty list Invalid index. Index must be >=0 and <=size. (0, 1, 2, 3, 4, 5, 6) (0, 1, 2) (0, 1, 2) ***** BEGIN remove(int i) TEST ***** [@]:1 size: 5 [1]:2 size: 4 [2]:3 size: 3 [3]:4 size: 2 [4]:5 size: 1 [5]:6 size: 0 Empty list: () Size: 6 List before removals: (0, 1, 2, 3, 4, 5) size: 6 Here we show the “index” of the element we remove and size of list after removal [5]:5 size: 5 (0, 1, 2, 3, 4) [4]:4 size: 4 (@, 1, 2, 3) [3] :3 size: 3 (0, 1, 2) [2]:2 size: 2 (0, 1) [1]:1 size: 1 (0) [@]:0 size: Empty list: () Size: 0 ***** BEGIN getMiddle() TEST ***** Error triggered calling getMiddle() on empty list list must have 1 or more entries Test sizes up to 10 List: (1) getMiddle(): 1 List: (1, 2) getMiddle(): 1 List: (1, 2, 3) getMiddle(): 2 List: (1, 2, 3, 4) getMiddle(): 2 List: (1, 2, 3, 4, 5) getMiddle(): 3 List: (1, 2, 3, 4, 5, 6) getMiddle(): 3 List: (1, 2, 3, 4, 5, 6, 7) getMiddle(): 4 List: (1, 2, 3, 4, 5, 6, 7, 8) getMiddle(): 4 List: (1, 2, 3, 4, 5, 6, 7, 8, 9) getMiddle(): 5 List: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) getMiddle(): 5 ***** BEGIN ForwardIterator TEST ***** Here is test of size 0. (Expect no output). Output: Size: 1 (1) Size: 2 (1, 2) Size: 3 (1, 2, 3) Size: 4 (1, 2, 3, 4) Size: 5 (1, 2, 3, 4, 5) Show transcribed image text Your Tasks Complete the following tasks. When you are finished you should be able to run the provided tester and get correct output for all tasks. a. Complete the penultimateNode () method. (See exercise R-3.6). b. Complete a reverse () method for the SinglyLinkedList class. Empty method has been provided for you. C. Add a addSecond (E e) method – If list is empty, throw exception. Parameter e will be the second element in the list after this operation has completed. This method should return void. d. Complete an add (E e, int i) method to the SinglyLinkedList class. What is the run time of this method? Throw an exception ifi is an invalid index (we covered an appropriate exception in the scoreboard example in class). (i must be >= 0 and =0 and =0 and

Expert Answer


Answer to Please Use Java and please show your output too. public class SinglyLinkedList implements Cloneable, Iterable { //—–…

OR