Question: Q) To class SinglyLinkedList, add method insertInMiddle(E e) that inserts a new node in the middle of the list ( list should have at least
Q) To class SinglyLinkedList, add method insertInMiddle(E e) that inserts a new node in the middle of the list ( list should have at least two nodes initially, if not just display an error message) if the list has odd number of nodes, insert after the one in the middle.
(Here is the code) please solve it corectly
thanks
-------------------------------------------------------------
public class SinglyLinkedList
public SinglyLinkedList() { }
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
public E first() { if (isEmpty()) return null; return head.getData(); }
public E last() { if (isEmpty()) return null; return head.getData(); }
public void addFirst(E e) { head = new Node<>(e, head); if (size == 0) tail = head; size++;
}
public void addLast(E e) { Node
tail = newest; size++; }
public E removeFirst() { if (isEmpty()) return null; E answer = head.getData(); head = head.getNext(); size--; if (size == 0) tail = null; return answer; }
public E removeLast() { if (isEmpty()) return null; E answer = tail.getData(); if (head == tail) head = tail = null; else { Node
public void display() { for (Node
public class Node
public Node(E d, Node
public E getData() { return data; }
public Node
public void setNext(Node
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
