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, 21.06.2019 16:50
3.2.5 suppose that we have an estimate ahead of time of how often search keys are to be accessed in a bst, and the freedom to insert items in any order that we desire. should the keys be inserted into the tree in increasing order, decreasing order of likely frequency of access, or some other order? explain your answer.
Answers: 1
question
Computers and Technology, 22.06.2019 10:00
Jackson is teaching the decimal number system. he wants his students to know how to expand numbers by powers of 10. which is the correct order in which digits are assigned values in the decimal number system?
Answers: 1
question
Computers and Technology, 23.06.2019 16:00
Which analyst position analyzes information using mathematical models to business managers make decisions?
Answers: 1
question
Computers and Technology, 23.06.2019 18:40
How does is make you feel when you're kind to others? what are some opportunities in your life to be more kind to your friends and loved ones? imagine a world where kindness has be outlawed. how would people act differently? would your day-to-day life change significantly? why or why not?
Answers: 2
You know the right answer?
Please in Java, with any comments! Thank you. In the LinkedList class, write the new method public...
Questions
question
History, 15.04.2020 21:23
Questions on the website: 13722360