in this program we need to add these methods to an existing linkedlist. please h
ID: 3543081 • Letter: I
Question
in this program we need to add these methods to an existing linkedlist.
please help!
1. addList: This method is supposed to take another linked list as a parameter, and add the contents of this linked list to the calling instance's linked list. Adding the elemnts should only be done if the element does not already exist. All elements to be added should be added at the end of the linked list. 2. subtractList: This method does the opposite of addList. It is to take another linked list as a parameter and remove the contents of this linked list from the calling instance's linked list. Elements are only removed if they exist. 3. reverseList() No parameters passed, but it should reverse the contents of the list.
Explanation / Answer
Sample output
Printing the contents of linked list 1
1 2 3 4 5 6
Printing the contents of linked list 2
7 6 8 4 3 10
Printing the contents of the Linked list 1 after the addition of unique elements of list 2
1 2 3 4 5 6 7 8 10
Printing the contents of linked list 1 after subtraction of elements from linked list 2
1 2 5
After reversing the contents of linked list 1, the values are
5 2 1
Code
import java.util.Collections;
import java.util.LinkedList;
public class MyLinkedList {
static LinkedList list1 = new LinkedList();
public static void main(String[] args) {
LinkedList list2 = new LinkedList();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
list1.add(6);
list2.add(7);
list2.add(6);
list2.add(8);
list2.add(4);
list2.add(3);
list2.add(10);
System.out.println("Printing the contents of linked list 1");
for (int i = 0; i< list1.size(); i++){
System.out.print(list1.get(i)+" ");
}
System.out.println();
System.out.println("Printing the contents of linked list 2");
for (int i = 0; i< list2.size(); i++){
System.out.print(list2.get(i)+" ");
}
System.out.println();
//calling the addlist method.
MyLinkedList list = new MyLinkedList();
list.addList(list2);
System.out.println("Printing the contents of the Linked list 1 after the addition of unique elements of list 2");
for (int i = 0; i< list1.size(); i++){
System.out.print(list1.get(i)+" " );
}
// notice that there were few elements in list 2 which were same as list 1. Those elements are not added.
System.out.println();
//Calling the subtractList() method
list.subtractList(list2);
System.out.println("Printing the contents of linked list 1 after subtraction of elements from linked list 2");
for (int i = 0; i< list1.size(); i++){
System.out.print(list1.get(i)+" " );
}
System.out.println();
System.out.println("After reversing the contents of linked list 1, the values are ");
list.reverseList(list1);
for (int i = 0; i< list1.size(); i++){
System.out.print(list1.get(i)+" " );
}
}
public LinkedList addList(LinkedList list2){
for (int i = 0; i< list2.size(); i++){
if (!this.list1.contains(list2.get(i))){
list1.add(list2.get(i));
}
}
return list1;
}
public LinkedList subtractList(LinkedList list2){
for (int i = 0; i< list2.size(); i++){
if (this.list1.contains(list2.get(i))){
list1.remove(list2.get(i));
}
}
return list1;
}
public LinkedList reverseList(LinkedList list){
Collections.reverse(list);
return list;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.