Question: I am trying to implement a generic linked list. It works fine except for a small detail. When I attempt to use the insert method

I am trying to implement a generic linked list. It works fine except for a small detail. When I attempt to use the insert method it will only work properly if both linked list are of the same type (two linked list of String or two linked lists of Integer for example) However, if i try to insert a String linked list into an Integer linked list i get an error. I do not understand why this happens, I thought that my use of generic nodes and declaring my lists as GenericLinkedList list1 = new GenericLinkedList<>(); and GenericLinkedList list2 = new GenericLinkedList<>(); would make it such taht I would not be having the problem I am having.

A more detailed description of the method: receives a generic List (a Java List) and an index position as parameters, and copies each value of the passed list into the current list starting at the index position, provided the index position does not exceed the size. For example, if list has a,b,c and another list having 1,2,3 is inserted at position 2, the list becomes a,b,1,2,3,c

in my program if list 1 is 1 1 1 1 11 1 and i insert 3 3 3 it will work but if my list is 1 1 1 1 1 1 and i insert a b c i get an error.... why ?? What do I need to do to fix this? Ive included the class headers in my code below.

Thanks!

public class DriverMain {

public static void main (String [] args) {

GenericLinkedList list1 = new GenericLinkedList<>();

GenericLinkedList list2 = new GenericLinkedList<>();

list1.addToFront("a");

list1.addToFront("b");

list1.addToFront("c"); /// and so on.....

list2.addToFront(1);

list2.addToFront(2);

list2.addToFront(3);

list1.insert(list2, 2);

}

}

public class Node {

// data and pointer fields here

}

public class GenericLinkedList {

// all methods including addToFront(Node L, int i);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!