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 00:40
Reading characters and strings from the keyboard: consider the following c++ program 1. #include 2. #include 3. using namespace std; 4. mystring1 5. 6. int main() 7. { 8. 9. string mystring1, mystring2; mychar1 10. 11. 12. char mychar1, mychar2; 13. 14. cout< < "enter a string: "; mychar2 15. 16. cin> > mystring1; // 17. cin.get(mychar1); 18. cin> > mychar2; 19. getline(cin,mystring2); mystring2 20. 21. 22. cout<
Answers: 1
question
Computers and Technology, 22.06.2019 08:00
Apex q: what does a low employment rate indicate? a. not many people are earning high salaries b. not many people are going to college c. not many people are renting their homes d. not many people have jobs
Answers: 2
question
Computers and Technology, 23.06.2019 19:30
Anul 2017 tocmai s-a încheiat, suntem trişti deoarece era număr prim, însă avem şi o veste bună, anul 2018 este produs de două numere prime, 2 şi 1009. dorel, un adevărat colecţionar de numere prime, şi-a pus întrebarea: “câte numere dintr-un interval [a,b] se pot scrie ca produs de două numere prime? “.
Answers: 1
question
Computers and Technology, 25.06.2019 02:30
What group of plants produce fruit? question 2 options: gymnosperms nonvascular plants seedless vascular plants angiosperms
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, 04.07.2019 13:30