9

I would like to know how to create a linked list of linked lists. Also, It would be helpful if the predefined LinkedList (class from Java) and its methods are used for defining and for other add, get, listIterating operations.

2
  • Its even ok, if i get code of user-defined linkedlist class with basic methods. Thanks Jun 15, 2012 at 12:12
  • You don't need to comment on your post in this fashion. You can edit your original question at any time. Jun 15, 2012 at 14:29

4 Answers 4

25

You can put any object in a list, including another list.

LinkedList<LinkedList<YourClass>> list = new LinkedList<LinkedList<YourClass>>();

is a LinkedList of LinkedLists of YourClass objects. It can also be written in a simplified way since Java 7:

LinkedList<LinkedList<YourClass>> list = new LinkedList<>();

Very simple examples of manipulating such a list:

You then need to create each sublist, here adding a single sublist:

list.add(new LinkedList<YourClass>());

Then create the content objects:

list.get(sublistIndex).add(new YourClass());

You can then iterate over it like this (sublists' items are grouped by sublist):

for(LinkedList<YourClass> sublist : list) {
    for(YourClass o : sublist) {
        // your code here
    }
}

If you want to add specific methods to this list of lists, you can create a subclass of LinkedList (or List, or any other List subclasses) or you can create a class with the list of lists as a field and add methods there to manipulate the list.

2
  • and how to iterate over this list of list ?? Jul 20, 2017 at 9:54
  • @roottraveller Added a simple example :) There are many ways this can be done, depending on what you want to do.
    – Autar
    Jul 20, 2017 at 14:05
2

Well i've done this code and i've got it right

          java.util.LinkedList mainlist = new java.util.LinkedList();

          java.util.LinkedList sublist1 = new java.util.LinkedList();
          sublist1.add(object1);
          sublist1.add(object2);
          sublist1.add(object3);

          java.util.LinkedList sublist2=new java.util.LinkedList();
          sublist2.add(1);
          sublist2.add(2);

          mainlist.add(sublist1);
          mainlist.add(sublist2);

          // To retrieve the sublist1 from mainlist...........
          java.util.LinkedList temp = (java.util.LinkedList)mainlist.get(0);

Here variable mainlist is LinkedList of LinkedLists and variable temp contains the value the first list stored i.e sublist1..

1

You can even simplify access to the secondary lists, e.g. using

    final List<List<String>> lists = new LinkedList<List<String>>() {
        @Override
        public List<String> get(final int index) {
            while (index >= size()) {
                add(new LinkedList<>());
            }
            return super.get(index);
        }
    };

This code automatically adds new LinkedLists to the outer list. With this code you can later easily add single values:

lists.get(2).add("Foo");
0
LinkedList<LinkedList<YourClass>> yourList = new LinkedList<LinkedList<YourClass>>();

As the declaration. To add another linked list (to the end by default) you would do

yourList.add(new LinkedList<YourClass>());

To add an element to lets say the second linked list in the series:

yourList.get(1).add(new YourClass());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.