Linked List in another Linked List

Hi guys! I have a question for you. I'm studying programming alone so I don't know who else to ask. Now I'm on the chapter about Linked Lists. There is and exercise in which I need to use Linked List in another Linked List. But the example that is shown in my book is too complicated and not understandable. It says to define a new name of a type for lists:
define List<int> Dlist;
and with this I'm supposed to make this List:
List<Dlist> A;
I think it's a bit longer to say all I have to say here so I would better ask you how you would do this task. Also I will be thankful if you can give me a good source to read about this type of tasks. :)

Greetings :)
Let's change the question. Let's say that I define a new type:
define List<int> Dlist;
Then if I try to use a function which is not a member in the class of the list it gives me error if I try to use the data of the node because the data members of the class are private:
1
2
3
4
void func(Dlist A, Dlist B)
  {
     List<T> *temp = B.start_ptr;//Error when I try to get the starting point of the list
  }


So my new question is how is this supposed to be solved? Is there any way?
Have you learned about inheritance yet?
I haven't reached it yet. But this exercise is before it. Is inheritance important for this type of tasks?
I was wrong by saying inheritance. This is an issue of the workings of OOP and classes in general. Read up on what private actually is, and how to interact with private members.

As a hint and starter, private data members can only be accessed by their owning class (or a friend of the owning class). It's data encapsulation, and can be very useful.

http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)

http://msdn.microsoft.com/en-us/library/zfbte35d.aspx
@OP: use the interface of the class, don't mess with its organs.

Given that a list is a container, it should provide some way to access its elements.
In the case of std::list, iterators are used


About your first post, you've got bricks inside boxes ¿what's the problem in putting those boxes inside a store room?
Last edited on
Here's a little code snippet to get your feet wet, as ne555 pointed out, we use iterators. You can refer to the documentation on this website or en.cppreference.com about linked lists. ne555 also mentions something I believe is important: to not mess with the insides. You are given this class and you have a high degree of freedom in using it, if you want more raw access you should code your own list class imho.

1
2
3
std::list<std::list<int>> L;
L.insert(L.begin(), std::list<int>());
L.begin()->insert(L.begin()->begin(), 32);
Topic archived. No new replies allowed.