[Upgrading, Modifying and Unlocking] donhashem.dh:
LESSON 15

Lesson 15 - Singely Linked List

Linked lists are a way to store data with structures so that the programmer can automatically create a new place to store data whenever necessary. Specifically, the programmer writes a struct or class definition that contains variables holding information about something, and then has a pointer to a struct of its type. Each of these individual struct or classes in the list is commonly known as a node.

Think of it like a train. The programmer always stores the first node of the list. This would be the engine of the train. The pointer is the connector between cars of the train. Every time the train adds a car, it uses the connectors to add a new car. This is like a programmer using the keyword new to create a pointer to a new struct or class.

In memory it is often described as looking like this:

Code: [Select]

---------- ----------

- Data - - Data -

---------- ----------

- Pointer- - - -> - Pointer-

---------- ----------

The representation isn't completely accurate, but it will suffice for our purposes. Each of the big blocks is a struct (or class) that has a pointer to another one. Remember that the pointer only stores the memory location of something, it is not that thing, so the arrow goes to the next one. At the end, there is nothing for the pointer to point to, so it does not point to anything, it should be a null pointer or a dummy node to prevent it from accidentally pointing to a totally arbitrary and random location in memory (which is very bad).

So far we know what the node struct should look like:

Code: [Select]

struct node {

int x;

node *next;

};

int main()

{

node *root; // This will be the unchanging first node

root = new node; // Now root points to a node struct

root->next = 0; // The node root points to has its next pointer

// set equal to a null pointer

root->x = 5; // By using the -> operator, you can modify the node

// a pointer (root in this case) points to.

}

This so far is not very useful for doing anything. It is necessary to understand how to traverse (go through) the linked list before going further.

Think back to the train. Lets imagine a conductor who can only enter the train through the engine, and can walk through the train down the line as long as the connector connects to another car. This is how the program will traverse the linked list. The conductor will be a pointer to node, and it will first point to root, and then, if the root's pointer to the next node is pointing to something, the "conductor" (not a technical term) will be set to point to the next node. In this fashion, the list can be traversed. Now, as long as there is a pointer to something, the traversal will continue. Once it reaches a null pointer (or dummy node), meaning there are no more nodes (train cars) then it will be at the end of the list, and a new node can subsequently be added if so desired.

Here's what that looks like:

Code: [Select]

struct node {

int x;

node *next;

};

int main()

{

node *root; // This won't change, or we would lose the list in memory

node *conductor; // This will point to each node as it traverses the list

root = new node; // Sets it to actually point to something

root->next = 0; // Otherwise it would not work well

root->x = 12;

conductor = root; // The conductor points to the first node

if ( conductor != 0 ) {

while ( conductor->next != 0)

conductor = conductor->next;

}

conductor->next = new node; // Creates a node at the end of the list

conductor = conductor->next; // Points to that node

conductor->next = 0; // Prevents it from going any further

conductor->x = 42;

}

That is the basic code for traversing a list. The if statement ensures that there is something to begin with (a first node). In the example it will always be so, but if it was changed, it might not be true. If the if statement is true, then it is okay to try and access the node pointed to by conductor. The while loop will continue as long as there is another pointer in the next. The conductor simply moves along. It changes what it points to by getting the address of conductor->next.

Finally, the code at the end can be used to add a new node to the end. Once the while loop as finished, the conductor will point to the last node in the array. (Remember the conductor of the train will move on until there is nothing to move on to? It works the same way in the while loop.) Therefore, conductor->next is set to null, so it is okay to allocate a new area of memory for it to point to. Then the conductor traverses one more element (like a train conductor moving on the the newly added car) and makes sure that it has its pointer to next set to 0 so that the list has an end. The 0 functions like a period, it means there is no more beyond. Finally, the new node has its x value set. (It can be set through user input. I simply wrote in the '=42' as an example.)

To print a linked list, the traversal function is almost the same. It is necessary to ensure that the last element is printed after the while loop terminates.

For example:

Code: [Select]

conductor = root;

if ( conductor != 0 ) { //Makes sure there is a place to start

while ( conductor->next != 0 ) {

cout<< conductor->x;

conductor = conductor->next;

}

cout<< conductor->x;

}

The final output is necessary because the while loop will not run once it reaches the last node, but it will still be necessary to output the contents of the next node. Consequently, the last output deals with this. Because we have a pointer to the beginning of the list (root), we can avoid this redundancy by allowing the conductor to walk off of the back of the train. Bad for the conductor (if it were a real person), but the code is simpler as it also allows us to remove the initial check for null (if root is null, then conductor will be immediately set to null and the loop will never begin):

Code: [Select]

conductor = root;

while ( conductor != NULL ) {

cout<< conductor->x;

conductor = conductor->next;

}

LESSON 16

Recursion is defined as a function calling itself. It is in some ways similar to a loop because it repeats the same code, but it requires passing in the looping variable and being more careful. Many programming languages allow it because it can simplify some tasks, and it is often more elegant than a loop.

A simple example of recursion would be:

Code: [Select]

void recurse()

{

recurse(); //Function calls itself

}

int main()

{

recurse(); //Sets off the recursion

}

This program will not continue forever, however. The computer keeps function calls on a stack and once too many are called without ending, the program will crash. Why not write a program to see how many times the function is called before the program terminates?

Code: [Select]

#include <iostream>

using namespace std;

void recurse ( int count ) // Each call gets its own count

{

cout<< count <<"\n";

// It is not necessary to increment count sinceeach function's

// variables are separate (so each count will be initialized one greater)

recurse ( count + 1 );

}

int main()

{

recurse ( 1 ); //First function call, so it starts at one

}

This simple program will show the number of times the recurse function has been called by initializing each individual function call's count variable one greater than it was previous by passing in count + 1. Keep in mind, it is not a function restarting itself, it is hundreds of functions that are each unfinished with the last one calling a new recurse function.

It can be thought of like the Russian dolls that always have a smaller doll inside. Each doll calls another doll, and you can think of the size being a counter variable that is being decremented by one.

Think of a really tiny doll, the size of a few atoms. You can't get any smaller than that, so there are no more dolls. Normally, a recursive function will have a variable that performs a similar action; one that controls when the function will finally exit. The condition where the functin will not call itself is termed the base case of the function. Basically, it is an if-statement that checks some variable for a condition (such as a number being less than zero, or greater than some other number) and if that condition is true, it will not allow the function to call itself again. (Or, it could check if a certain condition is true and only then allow the function to call itself).

A quick example:

Code: [Select]

void doll ( int size )

{

if ( size == 0 ) // No doll can be smaller than 1 atom (10^0==1) so doesn't call itself

return; // Return does not have to return something, it can be used

// to exit a function

doll ( size - 1 ); // Decrements the size variable so the next doll will be smaller.

}

int main()

{

doll ( 10 ); //Starts off with a large doll (its a logarithmic scale)

}

This program ends when size equals one. This is a good base case, but if it is not properly set up, it is possible to have an base case that is always true (or always false).

Once a function has called itself, it will be ready to go to the next line after the call. It can still perform operations. One function you could write could print out the numbers 123456789987654321. How can you use recursion to write a function to do this? Simply have it keep incrementing a variable passed in, and then output the variable...twice, once before the function recurses, and once after...

Code: [Select]

void printnum ( int begin )

{

cout<< begin;

if ( begin > 9 ) // The base case is when begin is greater than 9

printnum ( begin + 1 ); // for it will not recurse after the if-statement

cout<< begin; // Outputs the second begin, after the program has

// gone through and output

}

This function works because it will go through and print the numbers begin to 9, and then as each printnum function terminates it will continue printing the value of begin in each function from 9 to begin.

This is just the beginning of the usefulness of recursion. Heres a little challenge, use recursion to write a program that returns the factorial of any number greater than 0. (Factorial is number*number-1*number-2...*1).

Hint: Recursively find the factorial of the smaller numbers first, ie, it takes a number, finds the factorial of the previous number, and multiplies the number times that factorial.

LESSON 17

Lesson 17 - Func Var Arg

Perhaps you would like to have a function that will accept any number of values and then return the average. You don't know how many arguments will be passed in to the function. One way you could make the function would be to accept a pointer to an array. Another way would be to write a function that can take any number of arguments. So you could write avg(4, 12.2, 23.3, 33.3, 12.1); or you could write avg(2, 2.3, 34.4); Some library functions can accept a variable list of arguments (such as the venerable printf).

To use a function with variable number of arguments, or more precisely, a function without a set number of arguments, you would use the cstdarg header file. There are four parts needed: va_list, which stores the list of arguments, va_start, which initializes the list, va_arg, which returns the next argument in the list, and va_end, which cleans up the variable argument list. Whenever a function is declared to have an indeterminate number of arguments, in place of the last argument you should place an ellipsis (which looks like '...'), so, int a_function ( int x, ... ); would tell the compiler the function should accept however many arguments that the programmer uses, as long as it is equal to at least one, the one being the first, x.

va_list is like any other variable. For example,

Code: [Select]

va_list a_list;

va_start is a macro which accepts two arguments, a va_list and the name of the variable that directly precedes the ellipsis (...). So, in the function a_function, to initialize a_list with va_start, you would write va_start ( a_list, x );

va_arg takes a va_list and a variable type, and returns the next argument in the list in the form of whatever variable type it is told. It then moves down the list to the next argument. For example, va_arg ( a_list, double ) will return the next argument, assuming it exists, in the form of a double. The next time it is called, it will return the argument following the last returned number, if one exists.

To show how each of the parts works, take an example function:

Code: [Select]

#include <cstdarg>

#include <iostream>

using namespace std;

double average ( int num, ... )

{

va_list arguments; // A place to store the list of arguments

double sum = 0;

va_start ( arguments, num ); // Initializing arguments to store all values after num

for ( int x = 0; x < num; x++ ) // Loop until all numbers are added

sum += va_arg ( arguments, double ); // Adds the next value in argument list to sum.

va_end ( arguments ); // Cleans up the list

return sum / num; // Returns some number (typecast prevents truncation)

}

int main()

{

cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl;

cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl;

}

It isn't necessarily a good idea to use a variable argument list at all times, because the potential exists for assuming a value is of one type, while it is in fact another, such as a null pointer being assumed to be an integer. Consequently, variable argument lists should be used sparingly.