C++ - Linked list – recursive functions « Coding Friends

4
A linked list, is basically a list of elements that have a link attached to each one to the next. So for example if you have a structure like struct List { string name; List *next; }; 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; }; // inserts a Elem into a linked list void setUpElem(Elem * & list, Elem *addnew) { addnew->next = list; list = addnew; } .. 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; } // recursive loop to print them all out void printThemAllOut(Elem *list) { if (list != NULL) { printOutElem(list); printThemAllOut(list->next); } } printOutElem will print out the element name associated with the parameter, and the cool recursive function called

Transcript of C++ - Linked list – recursive functions « Coding Friends

Page 1: C++ - Linked list – recursive functions « Coding Friends

A linked list, is basically a list of elements that have a link attached to each one to the next. So for example if youhave a structure like

struct List { string name; List *next;};

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 oflinked List types. I have called it a Elem in the source code below because then it may make more sense, since it iskinder 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 listthen need to pass as a reference (&) so we actually alter the main variable and not just the functions variable and itis not passed back.

struct Elem {string name;Elem *next;

}; // inserts a Elem into a linked listvoid setUpElem(Elem * & list, Elem *addnew){

addnew->next = list;list = addnew;

} .. 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 addressmemory 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;} // recursive loop to print them all outvoid printThemAllOut(Elem *list){

if (list != NULL){

printOutElem(list);printThemAllOut(list->next);

}}

printOutElem will print out the element name associated with the parameter, and the cool recursive function called

Page 2: C++ - Linked list – recursive functions « Coding Friends

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, butin the middle, like it is sorted, then if you was using loops you would require to keep hold of the previous element sothat you can insert the new element into that previous elements -> next link and re-link the inserted element -> nextto the current element in the list, hopefully the code helps to understand..

void insertIntoMiddle(Elem *&elem, Elem *midElem){

Elem *cur, *pre= NULL;

for (cur=elem; cur != NULL; cur= cur->next){

if (cur->name > midElem->name) break;// store where we waspre = cur;

}

// place the middle next element equal to where we are in the listmidElem->next = cur;// if the previous is not null, e.g. previous was somewhere in the listif (pre != NULL)

pre->next = midElem; // update the previous link to the middle elementelse

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 withpassing the previous link to itself so there is no need to keep a element of the previous link, very nice and allot moreeasier 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);}

here is the full source code

#include <string>#include <iostream> using namespace std; // the element structure, the name is the name of the person// and the next is a link to next namestruct Elem {

string name;Elem *next;

}; // inserts a Elem into a linked listvoid setUpElem(Elem * & list, Elem *addnew){

addnew->next = list;list = addnew;

} // just print out the top element void printOutElem(Elem *elem){

Page 3: C++ - Linked list – recursive functions « Coding Friends

cout << "Name : " << elem->name << endl;} // recursive loop to print them all outvoid printThemAllOut(Elem *list){

if (list != NULL){

printOutElem(list);printThemAllOut(list->next);

}} // you have to keep in contact with the current and previous // (since that is where we need to place the new elementvoid insertIntoMiddle(Elem *&elem, Elem *midElem){

Elem *cur, *pre= NULL;

for (cur=elem; cur != NULL; cur= cur->next){

if (cur->name > midElem->name) break;// store where we waspre = cur;

}

// place the middle next element equal to where we are in the listmidElem->next = cur;// if the previous is not null, e.g. previous was somewhere in the listif (pre != NULL)

pre->next = midElem; // update the previous link to the middle elementelse

elem = midElem; // else probably is empty !!.. not good.. } // a far better way of doing this is to use recursive for inserting// since the parameter is the previous link :)// this is just a far nicer way of doing the above functionvoid insertIntoMiddleRecursive(Elem *&elem, Elem *midElem){

if (elem == NULL || elem->name > midElem->name){

midElem->next = elem;elem = midElem;

}else

insertIntoMiddleRecursive(elem->next, midElem);} int main(){

Elem *elem = NULL;

Elem newElem;newElem.name = "steve";setUpElem(elem, &newElem);Elem nextElem;nextElem.name = "genux";setUpElem(elem, &nextElem);

printThemAllOut(elem);

// now lets insert into the middle of the linked listElem midElem;midElem.name = "henry";insertIntoMiddle(elem, &midElem);

cout << "after the insert of \"henry\" into the middle" << endl;printThemAllOut(elem);

Page 4: C++ - Linked list – recursive functions « Coding Friends

Elem newMidElem;newMidElem.name = "janet";insertIntoMiddleRecursive(elem, &newMidElem);

cout << "after the insert of \"janet\" into the middle - using recursive :) " << endl;printThemAllOut(elem);

return 0;}

and here is the output

Name : genuxName : steveafter the insert of "henry" into the middleName : genuxName : henryName : steveafter the insert of "janet" into the middle - using recursive :)Name : genuxName : henryName : janetName : steve