Question: Need some help with this Java Assignment pertaining to Linked Lists. The goal of the assignment is to create a method that removes a given

Need some help with this Java Assignment pertaining to Linked Lists. The goal of the assignment is to create a method that removes a given element from the Linked List and a specified number of instances of that element.

Need some help with this Java Assignment pertaining to Linked Lists. The

Source Code:

import java.util.LinkedList;

public class Main { public static void main(String[] args) { /* theChain is 3, 4, 7, 6, 3, 2, 9, 6, 3, 6 */ java.util.LinkedList list = new java.util.LinkedList(); list.add(3); list.add(4); list.add(7); list.add(6); list.add(3); list.add(2); list.add(9); list.add(6); list.add(3); list.add(6);

System.out.println(list); } }

public class Node { private int element; private Node next; public Node(int element) { this.element = element; next = null; } public int getElement() { return element; } public void setElement(int element) { this.element = element; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } @Override public String toString() { return "" + element; } }

public class LinkedList {private Node head; private int size; public LinkedList() { head = null; size = 0; } public void add(int element) { Node newNode = new Node(element); if (head == null) { head = newNode; size = 1; } else { Node current = head; while (current.getNext() != null) { current = current.getNext(); } current.setNext(newNode); size++; } } public int getSize() { return size; } @Override public String toString() { String s = ""; if (head == null) { return s; } else { Node current = head; while (current.getNext() != null) { s += current.toString() + ", "; current = current.getNext(); } s += current.toString(); // last item has no trailing comma return s; } } public LinkedList removeN(LinkedList head, int val){ while (head != null && head.val==val){ head = head.next; } LinkedList currentNode = head; while (currentNode != null && currentNode.next != null){ if (currentNode.next.val == val){ currentNode.next = currentNode.next.next; }else{ currentNode = currentNode.next; } } return head; }

}

Fun with Linked Lists Purpose To practice writing code involving linked lists. Directions: First download the file LinkedList Practice Problem.zip and open it in the IDE of your choice, such as repl.it. Read through the code to make sure you understand how it works. Your task is to implement a public method in the LinkedList class called removeN that takes an integer called element that indicates the element to be removed from the list and an integer parameter n that indicates the number of copies of that element to remove, if possible. The method should return a boolean indicating whether or not the removeN operation was successful. Be sure to also update the size of the list appropriately. If there are at least n occurrences of the element in the list, then n nodes with that element are removed starting from the beginning of the list and moving to the right node by node and the method returns true. If there aren't enough occurrences of the element in the list, then the list should not be altered and the method should return false. Write a driver program that throughly tests your method. Examples: list is 3, 4, 7, 6, 3, 2, 9, 6, 3, 6 list.removeN(6, 2) returns true and list is now 3,4,7,3, 2, 9, 3, 6 list is 3, 4, 7, 6, 3, 2, 9, 6, 3,6 list.removeN(6, 4) returns false and list is still the same, because there are not four occurrences of the element 6 in the list

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!