Hi everyone,
i have a general question.
im creating a random class (person for example), and i want all persons to be stored in a linked list.
now, how do i do this? should i implement it in the person's class?
or should i create another class for the list?
should the linked linst be a person's data member?
The list would be a container for the person class. It is better when the person class does not know anything about the list.
Usually a list consists of the container and node. The container has a pointer to the head (and tail) node, while the node a pointer to the actual data (i.e. the person class).
The easiest thing is to just use std::list<>. If you really want to create your own list then I suggest 3 classes:
- class Person. As coder777 said, this just represents a person and has no knowledge of the list.
- class Node. This is a node in the list. It has a Person and a pointer to the next node.
- class List. This is the list itself. It has a Node pointer that is the head of the list, and all the methods for the list
Oh, you might want to add an iterator class also to go through the list.