May 17, 2024, 03:18:47 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Need a better Linked List example

Started by Bruce Peaslee, February 16, 2006, 11:25:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bruce Peaslee

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.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Ionic Wind Support Team

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.
Ionic Wind Support Team

Bruce Peaslee

Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Bruce Peaslee

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
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Parker


Bruce Peaslee

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.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Ionic Wind Support Team

The generic linked list classes have been in Aurora for a while.  The CPointerList is used in the frags2d.src example.
Ionic Wind Support Team

Bruce Peaslee

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.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Bruce Peaslee

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;
}
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Ionic Wind Support Team

Your making it hard on yourself though ;)

item = myList.Add(new(Restaurant,1));
Ionic Wind Support Team

Ionic Wind Support Team

item->sID   = "94591";
item->sName = "Chez What?";

Is generally more readable ;)
Ionic Wind Support Team

Bruce Peaslee

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.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Ionic Wind Support Team

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.

Ionic Wind Support Team

Bruce Peaslee

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;
}
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Parker

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).