Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please complete the code public class SinglyLinkedList t private static class No

ID: 3858743 • Letter: P

Question

Please complete the code

public class SinglyLinkedList t private static class Node private E value; private Node next; private Node(E value,Node next) this. value value; this.next next; // Instance variable private Node head; // SinglyLinkedList methods / returns true if the list is empty*/ public boolean isEmpty) return head null; public void addfirst(E item) t //this method will be use to populate the list if (item = null) { throw new NullPointerException("Illegal argument"); head new Node(item, head); //The recursive method size returns the number of nodes in the list public int size)t // the public size method calls a private recursive method size starting from head return size(head); private int size(Node p)t //complete this method / /The recursive method get returns the element at position index (first element is at position 0) public E get( int index ) if (index0) return null; else return .....i// make a call to the private recursive method

Explanation / Answer

Node.java:


public class Node<E> {
  
private E ele;
private Node<E> next;

  
public Node() {
this(null, null);
}

public Node(E ele, Node<E> next) {
this.ele = ele;
this.next = next;
}

public E getele() {
return ele;
}

public Node<E> getNext() {
return next;
}

public void setele(E ele) {
this.ele = ele;
}

public void setNext(Node<E> next) {
this.next = next;
}

}

SinglyLinkedList.java:

public class SinglyLinkedList<E> {
  
protected Node<E> head, tail;
protected long size;

  
public SinglyLinkedList() {
head = null;
tail = null;
size = 0;
}


public void addFirst(Node<E> Node) {
  
if (tail == null)
tail = Node;
Node.setNext(head);
head = Node;

  
size++;
}

  
public void addAfter(Node<E> currentNode, Node<E> newNode) {
if (currentNode == tail)
tail = newNode;
newNode.setNext(currentNode.getNext());
currentNode.setNext(newNode);


size++;
}


public void addLast(Node<E> Node) {
Node.setNext(null);
tail.setNext(Node);
tail = Node;
size++;
}

  
public Node<E> removeFirst() {
if (head == null)
System.err.println("Error");


Node<E> temp = head;

  
head = head.getNext();
temp.setNext(null);
size--;

return temp;

}

public Node<E> removingLast() {
  
Node<E> NodeBef;
Node<E> NodeToRemove;

  
if (size == 0)
System.err.println("Error");

  
NodeBef = getFirst();

for (int count = 0; count < size - 2; count++)
NodeBef = NodeBef.getNext();

  
NodeToRemove = tail;

  
NodeBef.setNext(null);
tail = NodeBef;
size--;

return NodeToRemove;
}

  
public void remove(Node<E> NodeToRemove) {
  
Node<E> NodeBef, currentNode;

  
if (size == 0)
System.err.println("Error");

  
currentNode = getFirst();
if (currentNode == NodeToRemove)
removeFirst();
currentNode = getLast();
if (currentNode == NodeToRemove)
removingLast();

  
if (size - 2 > 0) {
NodeBef = getFirst();
currentNode = getFirst().getNext();
for (int count = 0; count < size - 2; count++) {
if (currentNode == NodeToRemove) {
  
NodeBef.setNext(currentNode.getNext());
size--;
break;
}

  
NodeBef = currentNode;
currentNode = currentNode.getNext();
}
}

}

  
public Node<E> getFirst() {
return head;
}

public Node<E> getLast() {
return tail;
}

public long getSize() {
return size;
}
  
  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote