Escolar Documentos
Profissional Documentos
Cultura Documentos
then the List has a internals of a name which is of type string, and then a link (a pointer) to the next List in the list of
linked List types. I have called it a Elem in the source code below because then it may make more sense, since it is
kinder a element of the whole linked list.
So to create a I wrote a function to add a element (Elem) to the list already, since we are going to be altering the list
then need to pass as a reference (&) so we actually alter the main variable and not just the functions variable and it
is not passed back.
struct Elem {
string name;
Elem *next;
};
..
int main()
{
Elem *elem = NULL;
Elem newElem;
newElem.name = "bob";
setUpElem(elem, &newElem);
}
with the function call setUpElem, I am passing the address of the newElem because that is the pointer (the address
memory location).
So to start with the recursive functions, lets print out all of the elements,
// just print out the top element
void printOutElem(Elem *elem)
{
cout << "Name : " << elem->name << endl;
}
printOutElem will print out the element name associated with the parameter, and the cool recursive function called
printThemAllOut, which just re-calls itself with the link to the next element to print out.. just a nice little function..
The next recursive that makes allot more sense, is when you are adding a element instead to the head of the list, but
in the middle, like it is sorted, then if you was using loops you would require to keep hold of the previous element so
that you can insert the new element into that previous elements -> next link and re-link the inserted element -> next
to the current element in the list, hopefully the code helps to understand..
void insertIntoMiddle(Elem *&elem, Elem *midElem)
{
Elem *cur, *pre= NULL;
// place the middle next element equal to where we are in the list
midElem->next = cur;
// if the previous is not null, e.g. previous was somewhere in the list
if (pre != NULL)
pre->next = midElem; // update the previous link to the middle element
else
elem = midElem; // else probably is empty !!.. not good..
}
and here is a very nice recursive function that will do the same as above but just in recursive calls to itself with
passing the previous link to itself so there is no need to keep a element of the previous link, very nice and allot more
easier to debug since less code.
void insertIntoMiddleRecursive(Elem *&elem, Elem *midElem)
{
if (elem == NULL || elem->name > midElem->name)
{
midElem->next = elem;
elem = midElem;
}
else
insertIntoMiddleRecursive(elem->next, midElem);
}
// place the middle next element equal to where we are in the list
midElem->next = cur;
// if the previous is not null, e.g. previous was somewhere in the list
if (pre != NULL)
pre->next = midElem; // update the previous link to the middle element
else
elem = midElem; // else probably is empty !!.. not good..
}
int main()
{
Elem *elem = NULL;
Elem newElem;
newElem.name = "steve";
setUpElem(elem, &newElem);
Elem nextElem;
nextElem.name = "genux";
setUpElem(elem, &nextElem);
printThemAllOut(elem);
cout << "after the insert of \"henry\" into the middle" << endl;
printThemAllOut(elem);
Elem newMidElem;
newMidElem.name = "janet";
insertIntoMiddleRecursive(elem, &newMidElem);
cout << "after the insert of \"janet\" into the middle - using recursive :) " << endl;
printThemAllOut(elem);
return 0;
}