Question: IN JAVA Add methods removeElement(T value) that removes the element with the value value, and removeAtIndex(int index) that removes at the index index from the

IN JAVA

Add methods removeElement(T value) that removes the element with the value value, and removeAtIndex(int index) that removes at the index index from the singly linked list, to the class SinglyLinkedList.

/* Singly linked list * Implementation*/ public class SinglyLinkedList { private Node head; private int size; /* Constructor */ public SinglyLinkedList() { size = 0; } /* Returns a boolean indicating whether the list is empty */ public boolean isEmpty() { return size == 0; } /* Returns the number of elements in the list */ public int size() { return size; } /* Returns the first element in the list */ public T first() { if (this.isEmpty()) { return null; } else { return head.value; } } /* Returns the last element in the list */ public T last() { if (this.isEmpty()) { return null; } else { // traverse the linked list to find the tail Node currentNode = head; while(currentNode.next != null) { currentNode = currentNode.next;} return currentNode.value; } } /* Insert at head : add an element to the front of the list */ public void addAtHead(T value) { Node newest = new Node<>(); // allocate a new node newest.value = value; // insert the element newest.next = head; // have new node point to old head head = newest; // update head to point to new node size++; // linked list size increments } /* Insert at tail*/ public void addAtTail(T value) { Node newestNext = null; // Have new node point to null Node newest = new Node<>(newestNext); // allocate a new node newest.value = value; // insert the element // if there is no elements in the linked list if(size == 0) { head = newest; return; } // traverse the linked list to find the tail Node currentNode = head; while(currentNode.next != null) { currentNode = currentNode.next; } // currentNode.next = newest; // have old last node point to new node size++; // linked list size increments } /* Insert an element into the i-th element in the list */ public void addInMiddle(int index, T value) { // test index out of bounds if(index > this.size()) { System.out.println("Out of list bounds!"); return; } // if index = 1, = insert at head if(index == 1) { addAtHead(value); return; } // if index = i, = insert at the i-th element // find currentNode with index i Node currentNode = head; for(int i = 0; currentNode != null && (i+1)!= index-1; i++) { currentNode = currentNode.next; } // allocate a new node Node newNode = new Node(); // insert new element newNode.value = value; // have the new node point to the same next node newNode.next = currentNode.next; // have the current node point to the new node currentNode.next = newNode; // size++; // linked list size increments } /* Remove at head */ public T removeAtHead() { if(this.isEmpty()) { return null; } else { T removed = head.value; head = head.next; size--; return removed; } } /* Remove at tail */ public T removeAtTail() { if(this.isEmpty()) { return null; } // if linked list's size = 1, = remove from head if(this.size == 1) { T removed = removeAtHead(); } // allocate a temporary node Node currentNode = head; Node preCurrentNode = null; // keep a moving window scheme while(currentNode.next != null) { preCurrentNode = currentNode; currentNode = currentNode.next; } T removed = currentNode.value; // find the second last node then have it point to null preCurrentNode.next = null; size--; // linked list size decrements return removed; } public void printList() { Node current = head; System.out.print("List: "); while(current != null) { System.out.printf("%2d ", current.value); current = current.next; } System.out.printf(" "); } public void printList2() { System.out.print("List: "); for(Node current = head; current != null; current = current.next) { System.out.printf("%2d ", current.value); } System.out.printf(" "); } } 

public class Node { public T value; public Node next; // reference to next node /* Constructor */ public Node() { next = null; } public Node(Node next) { this.next = next; } public Node(T value, Node next) { this.value = value; this.next = next; } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!