Ok first project done. A linked list that uses recursion to add nodes, print them and search the list for specific values. I learned a lot doing it and can feel a few of the concepts really solidifying in my mind. I even had fun doing it. Guess I'm one of those people who enjoy programming. Tell me where there's room for improvement, I can take and even like constructive criticism, so don't hold back. Well here it is... Damn, I hope I'm doing this code tag thing right...
Originally Posted by jsrig88
I just noticed that when the print method reaches index number 75 it prints 0.0. Can't fiugure that one out, could you help please? =)
//Basic linked list using loops, structures, pointers and recursion.
using namespace std;
// my global variables
void buildList(int i);
int searchList(int i);
//initailizing my random number generator to the system clock... At least I think that's what it
//does... 60 to 75 percent sure.
//variables local to the main method. The root node and its values, the current node, an int I used for my while
//loop, and a counter for the number of elements in my list.
root = new listNode;
cNode = root;
int i = 0;
//while loop that uses my buildList() method to fill my linked list with 100 random numbers.
//resets the current node pointer to root, so my printList() method can use it to print out the full
cNode = root;
//again resets the current node pointer to root, this time for my searchList() method. Then asks the
//user to enter a number between 1 and 100, which is then passed into searchlist. This will tell you what
//random number is at what index number in the apropriate listNode object.
cout<< "Please enter a number between 1 and 100.\n";
cout<<"The number "<<searchList(i)<<" is found at index "<<i<<". \n";
//My list building method. Uses recursion to find the next NULL node on the list and add the number passed into
//it. Also adds the counter number to index, and increments counter by 1.
void buildList(int i)
if (cNode->next == 0)
cNode->index = counter;
cNode->contents = i;
//my Printlist method runs through the whole linked list using recursion, printing out the index number and the contents
//separated by a full stop. Then it starts a new line for the next node and continues until the whole Linked list is printed.
if (cNode->next != 0)
cout<<cNode->index<<". "<<cNode->contents<<" \n";
//this final method takes in a index number entered by the user, finds the listNode object with said number, and returns
int searchList(int i)
if(i != cNode->index)