I wrote this a while ago... you can take the source if you want, seeing as it's nothing more than a pretty standard linked list... and open source rules...
Code:
/*
linked.cpp => Linked Lists
These are notes for myself that will help with linked lists. It is a working
Linked list that, when run, will print out every List Item in the form of
it's number in the line.
Major_Small
*/
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int i; //loop control variable
struct list; //structure declaration - needed for typedef
typedef list *ptrList; //typedef - makes life easier
struct list //structure
{
int variable; //this list item's data
ptrList link; //pointer to next list item (note typedef use here)
};
ptrList curr; //current list item in focus
ptrList next; //next list item in line
ptrList head; //first list item in line
head=new list; //create first list item
curr=head; //point 'curr' to first list item
curr->link=NULL; //set first list item's pointer to NULL so it doesn't look
//for something that doesn't exist
for(i=0;i<25;i++) //runs 25 times
{
next=new list; //creates a new list item
curr->link=next; //current list item set to point to next list item
curr->variable=i+1; //current variable gets something
curr=next; //focus changed to next list item
}
/* AT THIS POINT, THE LINKED LIST IS COMPLETE - WHAT IS BELOW IS TO PRINT
OUT THE LINKED LIST. THE CONSTANTS IN THE LIST ARE THAT 'HEAD' IS THE FIRST
LIST ITEM, AND 'NEXT' IS THE LAST LIST ITEM. THE LAST LIST ITEM SHOULD HAVE
ITS POINTER POINTING TO NULL */
curr->link=NULL; //last list item's pointer set to NULL for same reason
curr=head; //current points back to first - NOT NECESSARY - for printing
for(i=0;i<25;i++) //runs 25 times to print
{
cout<<curr->variable<<endl; //print what is in curr (ini to head above)
curr=curr->link; //move focus along list
}
return 0;
}