Question
Please Use Java and please show your output too. public class SinglyLinkedList implements Cloneable, Iterable { //---------------- nested Node class ---------------- /** * Node of
Please Use Java and please show your output too.
public class SinglyLinkedList
/** The element stored at this node */ private E element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */ private Node
/** * 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 new node */ public Node(E e, Node
// 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 such node). * @return the following node */ public Node
// 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
// instance variables of the SinglyLinkedList /** The head node of the list */ private Node
/** The last node of the list */ private Node
/** 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 empty list
// 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 first element 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 last element if (isEmpty()) return null; return tail.getElement(); } /** * Returns (but does not remove) the penultimate node of the list. * @return node before the end of the list - exception if size penultimateNode( ) { //***** Complete this method ***** // return null; } /** * Returns (but does not remove) the penultimate element of the list. * @return element before the end of the list - exception if size (e, head); // create and link a new node 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 the list Node
/** * Removes and returns the first element of the list. * @return the removed element (or null if empty) */ public E removeFirst() { // removes and returns the first element if (isEmpty()) return null; // nothing to remove E answer = head.getElement(); head = head.getNext(); // will become null if list had only one node 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; // use nonparameterized 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 matched successfully }
@SuppressWarnings({"unchecked"}) public SinglyLinkedList
public int hashCode() { int h = 0; for (Node walk=head; walk != null; walk = walk.getNext()) { h ^= walk.getElement().hashCode(); // bitwise exclusive-or with element's code h = (h >> 27); // 5-bit cyclic shift of composite code } return h; }
/** * Produces a string representation of the contents of the list. * This exists for debugging purposes only. */ public String toString() { StringBuilder sb = new StringBuilder("("); Node
public boolean hasNext() { //***** Complete this method *****// return false; }
// Note: this method has undefined behavior if hasNext() return false public E next() { //***** Complete this method *****// return null; } } }
Your Output should look something like this.
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 andStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started