I've studied the database examples for help with my linked list to no avail.
I want to create a linked list where each node contains two strings. If I match the first string, it returns the second.
Parker posted a generic linked list sample you can use for that. Also in the CCL I beleive.
I haven't finished my generic one yet, currently only CStringList and CIntList are available.
Paul.
OK, I'll look for it.
I finally got around to looking it up. It's pretty good. There is a heavy use of pointers, but if you work your way slowly through the functions, and try to understand each one before moving on to the next, you will see eventually how useful pointers can be.
It really is a generic list-handling set of routines. My test case was a list of structures.
Here is the link:
http://www.ionicwind.com/forums/index.php?topic=39.0
Ahh! Did I include executable code? Here's a better formed version: http://www.ionicwind.com/forums/index.php?topic=57.msg449#msg449
Quote from: Parker on May 30, 2006, 04:37:09 PM
Ahh! Did I include executable code? Here's a better formed version: http://www.ionicwind.com/forums/index.php?topic=57.msg449#msg449
At a glance it looks the same.
I typed it into my test program - rather than cut and paste - because I wanted to try and understand how it worked.
The generic linked list classes have been in Aurora for a while. The CPointerList is used in the frags2d.src example.
Quote from: Ionic Wizard on May 31, 2006, 12:16:22 AM
The generic linked list classes have been in Aurora for a while.ÂÃ, The CPointerList is used in the frags2d.src example.
My printout of acommon.inc wasn't up to date :P
Now I'll play with the class.
This works. Am I using the class correctly?
// example of the CPointerList class
// Bruce Peaslee
#AutoDefine "Off"
struct Restaurant
{
ÂÃ, Ã‚Ã, string sID;
ÂÃ, Ã‚Ã, string sName;
}
global sub main()
{
ÂÃ, Ã‚Ã, CPointerList myList;
ÂÃ, Ã‚Ã, Restaurant *item;
ÂÃ, Ã‚Ã, Restaurant *node;
ÂÃ, Ã‚Ã, OpenConsole();
ÂÃ, Ã‚Ã, print ("Linked List\n");
ÂÃ, Ã‚Ã, myList.Create();
ÂÃ, Ã‚Ã, item = myList.Add(__new(TYPE_POINTER,len(Restaurant)));
ÂÃ, Ã‚Ã, *item.sIDÂÃ, Ã‚Ã, = "94591";
ÂÃ, Ã‚Ã, *item.sName = "Chez What?";
ÂÃ, Ã‚Ã, item = myList.Add(__new(TYPE_POINTER,len(Restaurant)));
ÂÃ, Ã‚Ã, *item.sIDÂÃ, Ã‚Ã, = "94120";
ÂÃ, Ã‚Ã, *item.sName = "Chez Hey!";
ÂÃ, Ã‚Ã, for(node = myList.GetFirst(); node <> 0; node = myList.GetNext(node))
ÂÃ, Ã‚Ã, {
ÂÃ, Ã‚Ã, Ã‚Ã, Ã‚Ã, item = myList.GetData(node);
ÂÃ, Ã‚Ã, Ã‚Ã, Ã‚Ã, print(*item.sID + " : " + *item.sName + "\n");
ÂÃ, Ã‚Ã, }
ÂÃ, Ã‚Ã, myList.RemoveAll();
ÂÃ, Ã‚Ã, while (GetKey() == "");
ÂÃ, Ã‚Ã, CloseConsole();
ÂÃ, Ã‚Ã, return 0;
}
Your making it hard on yourself though ;)
item = myList.Add(new(Restaurant,1));
item->sID = "94591";
item->sName = "Chez What?";
Is generally more readable ;)
Quote from: Ionic Wizard on May 31, 2006, 12:29:54 PM
item->sIDÂÃ, Ã‚Ã, = "94591";
item->sName = "Chez What?";
Is generally more readable ;)
It is. I was following Parker's syntax just to be sure I could get it to work.
Quote
Your making it hard on yourself though
item = myList.Add(new(Restaurant,1));
That's better. I used __New() because it was in the include file.
Unless you have a specific reason you will want to use the NEW operator instead of the __NEW function. The NEW operator handles class construction, calculation of sizes, etc.
Yes.
Here is the cleaned up code:
// example of the CPointerList class
// Bruce Peaslee
#AutoDefine "Off"
struct Restaurant
{
ÂÃ, Ã‚Ã, string sID;
ÂÃ, Ã‚Ã, string sName;
}
global sub main()
{
ÂÃ, Ã‚Ã, CPointerList myList;
ÂÃ, Ã‚Ã, Restaurant *item;
ÂÃ, Ã‚Ã, Restaurant *node;
ÂÃ, Ã‚Ã, OpenConsole();
ÂÃ, Ã‚Ã, print ("Linked List\n");
ÂÃ, Ã‚Ã, myList.Create();
ÂÃ, Ã‚Ã, item = myList.Add(new(Restaurant,1));
ÂÃ, Ã‚Ã, item->sIDÂÃ, Ã‚Ã, = "94591";
ÂÃ, Ã‚Ã, item->sName = "Chez What?";
ÂÃ, Ã‚Ã, item = myList.Add(new(Restaurant,1));
ÂÃ, Ã‚Ã, item->sIDÂÃ, Ã‚Ã, = "94120";
ÂÃ, Ã‚Ã, item->sName = "Chez Hey!";
ÂÃ, Ã‚Ã, for(node = myList.GetFirst(); node <> 0; node = myList.GetNext(node))
ÂÃ, Ã‚Ã, {
ÂÃ, Ã‚Ã, Ã‚Ã, Ã‚Ã, item = myList.GetData(node);
ÂÃ, Ã‚Ã, Ã‚Ã, Ã‚Ã, print(item->sID + " : " + item->sName + "\n");
ÂÃ, Ã‚Ã, }
ÂÃ, Ã‚Ã, myList.RemoveAll();
ÂÃ, Ã‚Ã, while (GetKey() == "");
ÂÃ, Ã‚Ã, CloseConsole();
ÂÃ, Ã‚Ã, return 0;
}
I wrote those back before the new and delete operators existed. And my linked list functions work, but the class doesn't, which is why I used the functions when writing the dictionary, stack, queue classes (which do work).