Here is some basic linked list theory as decribed to me by my CS201 instructor:
1) You will probably always need a dedicated pointer to the head node of the list.
(especially if you plan to add new nodes to the beginning of the list)
2) Having a pointer to the tail of the list is optional
(but is necessary if you plan to add new nodes at the end of the list)
3) Unlike an array, you will probably not be able to access nodes of the list directly.. you will have to implement a basic linked list traversal algorithm.. that will start either at the head node or tail node.. and traverse the list to the desired node:
Code:
node* Myclass::name_search(string target) //function will accept a string (for example) and will return a pointer to the desired node (struct) in the list of link
{
node *temp = head_ptr; //preserve head pointer integrity right away. (for this case assume head_ptr is a Myclass var and is not passed into this function)
while(temp && temp->name != target) //testing nodes will usually include at least two conditions.. one should always test to see if the node != NULL to prevent dereferrencing a NULL pointer
{
//Starting with the head node in this example (initially assigned to 'temp') start a cycle of dereferrincing, testing, and re-assigning temp until either the target is found.. or until end of list (NULL)
temp = temp->next; //target name was not found.. so make 'temp' the next node in the list.. and try again. simple pointer reassignment
}
return temp; //temp will either be the first node in the list that matches the target.. or temp will be NULL.
}
4) if you want to add nodes to the list.. you will probably use 'head node insertion' or 'tail node insertion' which will involve creating a new node.. and re-assigning the head_ptr (or tail_ptr) to point to that new node.. and then have that new node point to the previous head/tail pointer.
5) if you want to delete a node from the list.. you will probably either use "head node deletion" or "tail node deletion".. which will involve first re-assigning the head_ptr (or tail_ptr) and then deleteting the previous head/tail pointer.
the new and delete keywords are what make linked lists unique and very powerful when it comes to creating new objects (nodes) at runtime.. unlike arrays.
your class destructor.. will probably be nothing more than a loop that will call your "delete_node( )" function until there are no more nodes in the list.. so when your program exits, it will have returned all that memory created at runtime back to the freestore.
remember: always keeping track of the beginning and/or end of the list is half the battle when using a linked list.
tip: if you create a pointer.. and it is not initialized to anything specifically, always initialize it to NULL. (it's a bad idea just to declare a pointer variable.. and have it just exist without either pointing to something.. or being set to NULL)
finally: after you've written basic linked list functions for your program (adding nodes.. locating a desired node etc) remember to test your functions for the "empty list case".. or see how your functions react to having no nodes in the list (head_ptr = NULL) Testing for the 'empty list case' will ensure that you will not dereferrence a NULL pointer.. which is very not good.
hope this gets you pointed (pun) in the right direction