+ 2

Please help me understanding Linked Lists

Hey, i am trying to understand how to implement a linked lists myself and found this code online: public class ListL { static class ElementL { private Object element; // Content of the element private ElementL next; //reference to next El. public ElementL (Object o){ element = o; next = null; } } private ElementL head; public ListL (Object o){ head = new Element (o); } What follows in the code are methods. Now my QUESTION is Why do we declare another static class ElementL within ListL?

30th Jan 2018, 8:05 PM
Alexander E
Alexander E - avatar
3 Answers
+ 4
The ElementL class is inside the ListL class, because it shouldn't be accessed from outside. Only the ListL class needs to access the ElementL class.
30th Jan 2018, 8:23 PM
SplittyDev
SplittyDev - avatar
+ 1
Yes, you could also have the class outside the ListL class. But you still need a class to hold the nodes of the linked list. After all, a linked list has to keep track of the next node, so you need a way to store that information.
30th Jan 2018, 8:52 PM
SplittyDev
SplittyDev - avatar
0
@Splitty Dev thank you for your answer. Could i create a linked list without an inner class?
30th Jan 2018, 8:45 PM
Alexander E
Alexander E - avatar