You should conceptually split your linked list into two classes:
- Container
- Node
Linked List Container
The container contains a pointer to the first node, often called the head, and optionally, a pointer to the last node, often called the tail:
class Linked_List
{
Node * head;
Node * tail;
public:
Linked_List : head(nullptr), tail(nullptr)
{ ; }
};
Linked List Node
The node class contains pointer to the next node. In the case of a doubly linked list class, it contains a pointer to the previous node.
class Node
{
Node * next;
public:
Node() : next(nullptr)
{ ; }
void link_to(Node & other)
{
next = &other;
}
void remove_link()
{
next = nullptr;
}
};
Specializing the Node
You can specialize the node class by:
- Adding a data field to the class
- A new class that contains a data field and inherits from Node
- Make it a
template class.
Examples:
class Integer_Node_Inheritance : public Node
{
public:
int data;
};
class Node
{
Node * next;
int data;
public:
Node() : next(nullptr)
{ ; }
void link_to(Node & other)
{
next = &other;
}
void remove_link()
{
next = nullptr;
}
};
template <typename Data_Type>
class Node
{
Node * next;
Data_Type data;
public:
Node() : next(nullptr)
{ ; }
void link_to(Node & other)
{
next = &other;
}
void remove_link()
{
next = nullptr;
}
};
IntListconstructor, but you haven't shown enough code to be sure.