Monday, February 22, 2010

Recursion in Linked Lists

Around 30 mins into this video is a great explanation of recursion in linked lists:

In a nutshell, here is what the code might look like:

struct Student {
string name, address, phone;
Student *next;
};

What this allows for is the ability to enter the data for the Student structure, which then points to the next Student structure, and so forth, and so on.

Here is a revised copy of the code she uses in the video:
void PrintStudent(Student *s)
{
cout <<> name << " " <<>phone <<>
}

Student *GetNewStudent()
{
cout << "Enter name (Enter to quit):";
string name = GetLine();
if (name == "") return NULL;
Entry *newOne = new Student;

newOne->name = name; // same as *newOne.name;
// except that the arrow operator combines the dot and the //asterisk together to accomplish the same thing and //maintain order, otherwise it would need to look like this: //(*newOne).name;
cout << "Enter address:";
newOne->address = GetLine();
cout << "Enter phone:";
newOne->phone = GetLine();
newOne->next = NULL;
return newOne;
}

Student *BuildStudent()
{
Student *list = NULL;
while (true)
{
Student *newOne = GetNewEntry();
if (newOne == NULL) break;
newOne->next = list; // connects new node to next in list
list = newOne; // connects head of list to new node
}
return list;
}

int main ()
{
Student *n = GetNewStudent();
PrintStudent (n);
return 0;
}

Monday, February 15, 2010

Pointers and Heaps

The Heap
There are two memory places where variables reside: One is in the heap, a place that you may directly control, and the other is used directly when creating a variable:
int x = 0;

To access the heap, you first need to create a pointer through which you will obtain the address of that variable. You may do this in two steps, or in one:

int * pointer;
pointer = new int;

OR

int * pointer = new int;

Delete Heap Variables
These heap variables (new int) take up space in the heap memory. Once you no longer need that variable, you should deallocate the memory being used for that variable by typing:

delete pointer;

This allows the memory to be used for other things.

Nullify Heap Variables
Because a double deleted heap variable may cause your computer to crash, you should also nullify that variable immediately after deleting it:

delete pointer;
pointer = 0;

Heap Arrays
In order to work with arrays in heaps, you will need to first allocate memory for the array, shown here in one step rather than two:

int * ptr = new int[80];

When you are assigning individual elements of the array, you do so the same way you would a normal array:

ptr[1] = 20

Deleting Heap Arrays
When you are finished using the heap array, you will delete it thus:

delete[ ] ptr;

Heap Const Variables
A normal variable may take a const variable's value, but a const variable may not take non-const variable. The same applies for const pointers.

A const pointer will turn a non-const to a const.

Saturday, February 13, 2010

Follower Buttons!

Running on the White Line

Take this button:




Take the code box (shown above) for this button:

<div align=”center”>
<form> <textarea rows=”6″ cols=”19″ readonly=”readonly”><a href="http://www.runningonthewhiteline.com/" target="_blank"><img alt="Running on the White Line" src="http://i988.photobucket.com/albums/af5/nixiao/WhiteLine-2.png" border="0" /></a></textarea></form>
</div>


Running on the White Line

Take this button:




Take the code box (shown above) for this button:

<div align=”center”>
<form> <textarea rows=”6″ cols=”19″ readonly=”readonly”><a href="http://www.runningonthewhiteline.com/" target="_blank"><img alt="Running on the White Line" src="http://i988.photobucket.com/albums/af5/nixiao/whiteline3-1.png" border="0" /></a></textarea></form>
</div>


Running on the White Line

Take this button:




Take the code box (shown above) for this button:

<div align=”center”>
<form> <textarea rows=”6″ cols=”19″ readonly=”readonly”><a href="http://www.runningonthewhiteline.com/" target="_blank"><img alt="Running on the White Line" src="http://i988.photobucket.com/albums/af5/nixiao/whiteline2-1.png" border="0" /></a></textarea></form>
</div>


Notes from the Pear Tree House

Take this button:




Take the code box (shown above) for this button:

<div align=”center”>
<form> <textarea rows=”6″ cols=”19″ readonly=”readonly”><a href="http://notesfromthepeartree.blogspot.com/" target="_blank"><img alt="Notes from the Pear Tree House" src="http://i988.photobucket.com/albums/af5/nixiao/peartree2-1-2.png" border="0" /></a></textarea></form>
</div>


Notes from the Pear Tree House

Take this button:




Take the code box (shown above) for this button:

<div align=”center”>
<form> <textarea rows=”6″ cols=”19″ readonly=”readonly”><a href="http://notesfromthepeartree.blogspot.com/" target="_blank"><img alt="Notes from the Pear Tree House" src="http://i988.photobucket.com/albums/af5/nixiao/peartree3-1-1.png" border="0" /></a></textarea></form>
</div>


Notes from the Pear Tree House

Take this button:




Take the code box (shown above) for this button:

<div align=”center”>
<form> <textarea rows=”6″ cols=”19″ readonly=”readonly”><a href="http://notesfromthepeartree.blogspot.com/" target="_blank"><img alt="Notes from the Pear Tree House" src="http://i988.photobucket.com/albums/af5/nixiao/peartree1-1-1-1.png" border="0" /></a></textarea></form>
</div>


Notes from the Pear Tree House

Take this button:




Take the code box (shown above) for this button:

<div align=”center”>
<form> <textarea rows=”6″ cols=”19″ readonly=”readonly”><a href="http://notesfromthepeartree.blogspot.com/" target="_blank"><img alt="Notes from the Pear Tree House" src="http://i988.photobucket.com/albums/af5/nixiao/peartree4-1-2.png" border="0" /></a></textarea></form>
</div>


Apryl's Mind Showers

Take this button:




Take the code box (shown above) for this button:

<div align=”center”>
<form> <textarea rows=”6″ cols=”19″ readonly=”readonly”><a href="http://aprylsmindshowers.blogspot.com/" target="_blank"><img alt="Apryl's Mind Showers" src="http://i988.photobucket.com/albums/af5/nixiao/apryl3-1.png" border="0" /></a></textarea></form>
</div>


Apryl's Mind Showers

Take this button:




Take the code box (shown above) for this button:

<div align=”center”>
<form> <textarea rows=”6″ cols=”19″ readonly=”readonly”><a href="http://aprylsmindshowers.blogspot.com/" target="_blank"><img alt="Apryl's Mind Showers" src="http://i988.photobucket.com/albums/af5/nixiao/apryl3-1.png" border="0" /></a></textarea></form>
</div>


Apryl's Mind Showers

Take this button:




Take the code box (shown above) for this button:

<div align=”center”>
<form> <textarea rows=”6″ cols=”19″ readonly=”readonly”><a href="http://aprylsmindshowers.blogspot.com/" target="_blank"><img alt="Apryl's Mind Showers" src="http://i988.photobucket.com/albums/af5/nixiao/Apryl-1.png" border="0" /></a></textarea></form>
</div>