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>

Sunday, December 13, 2009

Machine Language Programming

Who would have thought that programming in Machine language could be so fun? Not really, but when you have the correct tables, and understand the basic concepts, I am sure it is not so difficult. Personally, just adding binary is complicated enough, but that too gets much better with experience.

Thursday, October 22, 2009

Const in a Nutshell

That heading sure could be misconstrued. Anyway, here are the rules of const:

1. Type const is used for unmodifiable objects.
2. Attempts to modify const objects will cause execution errors.
3. Member functions cannot call const objects unless the function is const.
4. Member functions that modify objects cannot be const.
5. Const member functions cannot call non-const object in the same class instance.
6. You can't use a non-const member function to call a const object.
7. One solution is to overload the const member function with a non-const version.
8. Constructors or destructors can never be const because they change objects.
9. Constructors may still be used to initialize const objects.
10. Const data members and data members referenced by const MUST be initialized in using member initializers.

Wednesday, October 21, 2009

Review of Pointers

Count counter; // create counter object
Count *counterPtr = &counter; // create pointer to counter
Count &counterRef = counter; // create reference to counter

cout << "Set x to 1 and print using the object's name: ";
counter.setX( 1 ); // set data member x to 1
counter.print(); // call member function print

cout << "Set x to 2 and print using a reference to an object: ";
counterRef.setX( 2 ); // set data member x to 2
counterRef.print(); // call member function print

cout << "Set x to 3 and print using a pointer to an object: ";
counterPtr->setX( 3 ); // set data member x to 3
counterPtr->print(); // call member function print

Funky Little ? Operator

cout << ( ( time == 0 time == 12 ) ? 12 )

This means that if time is 0 or time is 12, it will be represented with, or equal 12. Nifty!