I was thinking about storing the list as data inside the other list but unsure how to do that, if possible.
Possible with templates. Easy if you allow the 1,7,3 list to be 5,1,7,3
I'm just going to stick to the data structure and ignore the linked list logic since OP already has that written.
template<class TYPE>
class LinkedList
{
private:
struct Node
{
TYPE data;
Node * next;
Node * prev;
};
Node * head;
public:
LinkedList():head(nullptr)
{
}
~LinkedList()
{
while (head != nullptr)
{
Node *temp = head;
head = head->next;
delete temp;
}
}
// methods go here
};
This is the bare minimum. It needs insert, remove, and an iterator or other means of traversal to work with the list. To be Rule of Three compliant, it also needs a copy constructor and an assignment operator (operator=
). I recommend against exposing Node
to the user. With it they can wreak all sorts of unholy hell, so hide the Node
s behind iterators.
Declaration of the 5,1,7,3 variant list would look something like
LinkedList<LinkedList<int>> list;
list
points to a Node
. This Node
contains another LinkedList
that points to Node
s of int
s as well as the next and previous node.
To preserve 5,6,8,10 and 1,7,3, you need an intermediary that stores a number and a linked list
struct Intermediary
{
int data;
LinkedList<int> sublist;
};
LinkedList<Intermediary> list;
std::pair
would also work.