IonicWind Software

Aurora Compiler => General Discussion => Topic started by: Bruce Peaslee on February 16, 2006, 11:25:11 AM

Title: Need a better Linked List example
Post by: Bruce Peaslee on February 16, 2006, 11:25:11 AM
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.
Title: Re: Need a better Linked List example
Post by: Ionic Wind Support Team on February 16, 2006, 11:29:02 AM
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.
Title: Re: Need a better Linked List example
Post by: Bruce Peaslee on February 16, 2006, 11:34:26 AM
OK, I'll look for it.
Title: Re: Need a better Linked List example
Post by: Bruce Peaslee on May 30, 2006, 04:30:00 PM
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
Title: Re: Need a better Linked List example
Post by: 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
Title: Re: Need a better Linked List example
Post by: Bruce Peaslee on May 30, 2006, 11:08:25 PM
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.
Title: Re: Need a better Linked List example
Post by: Ionic Wind Support Team 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.
Title: Re: Need a better Linked List example
Post by: Bruce Peaslee on May 31, 2006, 10:48:46 AM
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.
Title: Re: Need a better Linked List example
Post by: Bruce Peaslee on May 31, 2006, 11:43:33 AM
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;
}
Title: Re: Need a better Linked List example
Post by: Ionic Wind Support Team on May 31, 2006, 12:28:53 PM
Your making it hard on yourself though ;)

item = myList.Add(new(Restaurant,1));
Title: Re: Need a better Linked List example
Post by: Ionic Wind Support Team on May 31, 2006, 12:29:54 PM
item->sID   = "94591";
item->sName = "Chez What?";

Is generally more readable ;)
Title: Re: Need a better Linked List example
Post by: Bruce Peaslee on May 31, 2006, 12:48:27 PM
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.
Title: Re: Need a better Linked List example
Post by: Ionic Wind Support Team on May 31, 2006, 12:51:14 PM
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.

Title: Re: Need a better Linked List example
Post by: Bruce Peaslee on May 31, 2006, 12:58:24 PM
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;
}
Title: Re: Need a better Linked List example
Post by: Parker on May 31, 2006, 03:28:35 PM
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).