Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Have a 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
Have a 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 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; } }
-----------------------------------------------------
public class Node
public T value;
public Node
/* Constructor */
public Node() {
next = null;
}
public Node(Node
this.next = next;
}
public Node(T value, Node
this.value = value;
this.next = next;
}
}
Step 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