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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 14:00, uday50
Select the correct answer. a company is currently focusing on creating specific management goals for itself. which level of maturity is the company demonstrating under the sse_ccm framework? a. performed informally b. planned and tracked c. quantitatively controlled d. well-defined e. continuously improving
Answers: 2
image
Computers and Technology, 24.06.2019 12:30, coursonianp8izbc
Do you think media is stereotype ? and why?
Answers: 1
image
Computers and Technology, 24.06.2019 14:00, Abrahamolve
When creating a field in a table, you must set the to determine what type of data the field can store. field property data type field type data property
Answers: 1
image
Computers and Technology, 24.06.2019 16:00, kamo90
How are roger williams, james oglethorpe, and william penn similar?
Answers: 3
You know the right answer?
Please in Java, with any comments! Thank you. In the LinkedList class, write the new method public...

Questions in other subjects:

Konu
Mathematics, 13.04.2020 13:52