Need help abstracting my linked list.
Okay, so here is my current code:
I don't want the Linked_List class to be an actual link in the linked list. Why should the links have functions to add more links?
Code:
#include <string>
#include <iostream>
using namespace std;
template< typename T_ >
class Linked_List
{
public:
Linked_List<T_>()
{
start_ptr = NULL;
current = NULL;
}
T_ data;
Linked_List<T_> * nxt;// Pointer to next node
void add_node(T_ & t)
{
Linked_List<T_> *temp, *temp2; // Temporary pointers
// Reserve space for new node and fill it with data
temp = new Linked_List<T_>;
temp->data = t;
temp->nxt = NULL;
// Set up link to this node
if (start_ptr == NULL)
{
start_ptr = temp;
current = start_ptr;
}
else
{
temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->nxt != NULL)
{
temp2 = temp2->nxt;
// Move to next link in chain
}
temp2->nxt = temp;
}
}
void display_list()
{
Linked_List<T_> *temp;
temp = start_ptr;
cout << endl;
if (temp == NULL)
cout << "The list is empty!" << endl;
else
{
while (temp != NULL)
{
// Display details for what temp points to
cout << temp->data;
if (temp == current)
cout << " <-- Current node";
cout << endl;
temp = temp->nxt;
}
}
}
void delete_start_node()
{
Linked_List<T_> *temp;
temp = start_ptr;
start_ptr = start_ptr->nxt;
delete temp;
}
void delete_end_node()
{
Linked_List<T_> *temp1, *temp2;
if (start_ptr == NULL)
cout << "The list is empty!" << endl;
else
{
temp1 = start_ptr;
if (temp1->nxt == NULL)
{
delete temp1;
start_ptr = NULL;
}
else
{
while (temp1->nxt != NULL)
{
temp2 = temp1;
temp1 = temp1->nxt;
}
delete temp1;
temp2->nxt = NULL;
}
}
}
void move_current_on ()
{
if (current->nxt == NULL)
current = start_ptr;
else
current = current->nxt;
}
void move_current_back ()
{
if (current == start_ptr)
cout << "You are at the start of the list" << endl;
else
{
node *previous; // Declare the pointer
previous = start_ptr;
while (previous->nxt != current)
{
previous = previous->nxt;
}
current = previous;
}
}
Linked_List<T_> * start_ptr;
Linked_List<T_> *current; // Used to move along the list
};
Here is a picture telling you what I want to do with it:
http://img.photobucket.com/albums/v1...ewhatimean.jpg
Basically, I want to make the Linked_List class more or less just a controller and entry point of the actual linked list.
I want to have an abstract node class that I can add functions to and derive those functions into subclasses.
By doing this I hope to accomplish a system where I can automatically and recursively update the data held in the links. Which should all be derivations of type link.
Does this make sense?