Search code examples
javalisthead

Doubly Linked List with different type of head


I want to make a doubly linked list Train where head is type of Locomotive and nodes are type of Wagon.

I thought about using templates, Train<T>, but I tried to use if(Train<T> cart instance of Locomotive) and failed, so that won't work...

Any ideas?


Solution

  • Make a LinkedList of type TrainCar as a superclass or interface, and then Locomotive and Wagon either extend or implement TrainCar.