java please Write a program using LinkedList and Listlterator to obtain the foll
ID: 3684625 • Letter: J
Question
java please
Write a program using LinkedList and Listlterator to obtain the following statements: Create a linked list named "number" with these elements: "one", "four" and "three". Create a List Iterator named "it 1" related to "number". Add the new element "two" after the element "one" Remove the element "four" Create a new List Iterator named "it2" related to "number", to add new elements in the respective positions. To create each new element you must use the value of the current element concatenated to " and a half'. The new obtained list is: one one and a half two two and a half three three and a halfExplanation / Answer
LinkedListProgram.java
package org.students;
import java.util.*;
public class LinkedListProgram {
public static void main(String[] args) {
//create LinkedList Object.
LinkedList number = new LinkedList();
//Adding elements to linked list
number.add("One");
number.add("four");
number.add("three");
System.out.print("Contents of Linked List:");
//Iterating over LinkedList
ListIterator it1 = number.listIterator();
while (it1.hasNext()) {
System.out.print(" " + it1.next() + " ");
}
number.add(1, "two");
number.remove("four");
System.out.println(" Contents of Linked List after deletion: "
+ number);
//Modifications done using Iterator
ListIterator it2 = number.listIterator();
while (it2.hasNext()) {
it2.next();
String str = (String) it2.previous();
it2.next();
it2.add(str + " and half");
}
System.out.println(" The new obtained List is: "
+ number);
}
}
_____________________________________________________________________________________________
output:
Contents of Linked List: One four three
Contents of Linked List after deletion: [One, two, three]
The new obtained List is: [One, One and half, two, two and half, three, three and half]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.