subject

Please in Java, with any comments! Thank you. In the LinkedList class, write the new method public void add(int index, E newValue). This method should add newValue to the list at the specified index. If the index is invalid for the list, throw an .
(You can delete and/or add on to the already made add method within this code, and if you perform any tests in the main method please show it, if not no worries, more curious for the actual method code than testing code. )
public class LinkedList implements List {
// This instance variable keeps track of the head node of the list.
// From that head node, we can get anywhere else in the list.
private Node head;
// This instance variable keeps track of how many elements are currently in the list
private int size = 0;
// Returns the list element at the specified index
public E get(int index) {
if (index >= 0 && index < size)
return nodeAt(index).getData();
else
throw new ();
}
// Replaces the list element at an existing index with a new value
public void set(int index, E newValue) {
if (index >= 0 && index < size)
nodeAt(index).setData(newValue);
else
throw new ();
}
// Adds a new element to the end of the list
public void add(E newValue) {
// Create a new node containing the newValue
Node newNode = new Node<>(newValue, null);
if (size == 0) { // If the list is empty...
// Point the head reference to the new node
head = newNode;
}
else { // If the list is not empty...
// Get to the last node in the list, and set its next to the new node
nodeAt(size - 1).setNext(newNode);
}
size++;
}
// Removes and returns the list element at the specified index
// Method stub - to be implemented later
public E remove(int index) {
return null;
}
public String toString() {
String result = "LinkedList object (size = " + size + "), elements: head -> ";
// The commented out loop below works, but it's inefficient because *every*
// call to nodeAt involves a loop
// for (int i = 0; i < size; i++)
// result += nodeAt(i).getData() + " -> ";
// Better - just a single loop through the list is needed
for (Node temp = head; temp != null; temp = temp. getNext())
result += temp. getData() + " -> ";
result += "null";
return result;
}
// Returns the Node object at the specified index in the list
// Declared private, since nodeAt is meant to be called only by other
// methods within this class
private Node nodeAt(int index) {
Node temp = head;
for (int i = 0; i < index; i++) // Runs for "index" iterations
temp = temp. getNext(); // Each time this runs, temp advances down the list by one node
return temp;
}
public static void main(String[] args) {
List test = new LinkedList<>();
System. out. println(test);
test. add(14);
System. out. println(test);
test. add(8);
System. out. println(test);
test. add(3);
System. out. println(test);
}
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 18:30
Which of the following commands is more recommended while creating a bot?
Answers: 1
question
Computers and Technology, 23.06.2019 08:00
The managing director of a company sends a christmas greeting to all his employees through the company email. which type of network does he use? he uses an .
Answers: 3
question
Computers and Technology, 24.06.2019 02:10
Which sentences describe the things you need to ensure while creating a sketch and a drawing? while an artistic or creative drawing is a creative expression, a technical drawing is an informative expression. you need to create accurate and neat drawings to convey accurate information. a technical drawing clearly conveys its meaning or information, and does not leave room for interpretation maintain a good speed while creating drawings
Answers: 1
question
Computers and Technology, 25.06.2019 01:00
What phrase indicates someone has knowledge and understanding of computer,internet,mobile devices and related technologies?
Answers: 1
You know the right answer?
Please in Java, with any comments! Thank you. In the LinkedList class, write the new method public...
Questions
question
Mathematics, 24.04.2020 11:03
question
Mathematics, 24.04.2020 11:03
question
Mathematics, 24.04.2020 11:07
question
Mathematics, 24.04.2020 11:09
question
Mathematics, 24.04.2020 11:16
Questions on the website: 13722363