Board index » delphi » Linked Lists in Delphi - how easy??

Linked Lists in Delphi - how easy??

In article <823080578...@rhmtech.demon.co.uk>,
Roger Staff  <roge...@rhmtech.demon.co.uk> wrote:

Quote
>Excuse what is probably a simple query - can I construct linked
>lists of records in the way I do in C using structures?

Yes.

Quote
>If so, are 'new' and 'dispose' equivalent to C's 'malloc' and
>'free' - if not, what would I use?  Or is there a better way??

Ermm, as I recall, malloc and free are more equivalent to GetMem and FreeMem -
you have to tell them how much memory to allocate and deallocate.  New and
Dispose are typed equivalents:  You just give them a pointer (or type) and
they know how much memory the datatype needs.  Less chance of error that
way.

Better way:  Maybe.  You might well find a TList more convenient than a linked
list.  The TList is an array of pointers, and all the maintenance code is
prewritten.  If you do a lot of sequential access, the array of pointers is
faster than a linked list; if you do a *lot* of random insertion and deletion,
the linked list is faster.

--

http://www.armory.com/~jon               Personal Pages
http://www.armory.com/~jon/pubs             Programming Publications
http://www.armory.com/~jon/hs         Home School Resource List

 

Re:Linked Lists in Delphi - how easy??


Excuse what is probably a simple query - can I construct linked
lists of records in the way I do in C using structures?

If so, are 'new' and 'dispose' equivalent to C's 'malloc' and
'free' - if not, what would I use?  Or is there a better way??

Thanks from someone gingerly making their way through Delphi....

--
Roger Staff

Re:Linked Lists in Delphi - how easy??


In article <823080578...@rhmtech.demon.co.uk>, roge...@rhmtech.demon.co.uk
says...

Quote

>Excuse what is probably a simple query - can I construct linked
>lists of records in the way I do in C using structures?

>If so, are 'new' and 'dispose' equivalent to C's 'malloc' and
>'free' - if not, what would I use?  Or is there a better way??

You are probably better off looking at a C++ Linked List example
than a C one.  Try creating a linked list class rather than a
record.  Use Create and Free to create the list.

John

Re:Linked Lists in Delphi - how easy??


Quote
Jon Shemitz wrote:

> In article <823080578...@rhmtech.demon.co.uk>,
> Roger Staff  <roge...@rhmtech.demon.co.uk> wrote:
> >Excuse what is probably a simple query - can I construct linked
> >lists of records in the way I do in C using structures?
> >If so, are 'new' and 'dispose' equivalent to C's 'malloc' and
> >'free' - if not, what would I use?  Or is there a better way??

Hi Jon,

You must use new() and dispose() only in combination with each other.

This is a modified extract of code I use in my 3D flight simulator written entirely in Delphi. I
use a linked list of records which contain coordinates for drawing lines.

1 - Start with defining the record, same as C STRUCT:

type
  linePtr = ^lineRec;
  lineRec = record
              next:   linePtr;        {points to next record}
              x1,y1,z1,               {contents of record}
              x2,y2,z2,
              colour: longInt;
            end;

2 - Define some global pointers:

var
  startLinePtr,                       {points to starts of linked list}
  currLinePtr: linePtr;               {pointer to 'walk' down link list}

In some 'init' routine make:
  startLinePtr:=nil;
  currLinePtr:=nil;
  lastLinePtr:=nil;

3 - Now write code to create a new record element on the global heap:

procedure createLineElement;
var
  tempPtr: linePtr;
begin
  if currLinePtr=nil then               {first element}
  begin
    new(currLinePtr);
    currLinePtr^.next:=nil;
    startLinePtr:=currLinePtr;
  end
  else
  begin                                 {insert element at end of list. My app is always at}
    tempPtr:=currLinePtr;               {the bottom of the list when this routine is called,}
    new(currLinePtr);                   {therefore I don't need to modify the next record}
    currLinePtr^.next:=nil;             {and always knowe I'm at the bottom of the list.}
    tempPtr^.next:=currLinePtr;
  end;
end;

4 - Access your code as your pointer as would normally:

    currLinePtr^.x1:=0;
    currLinePtr^.colour:=rgb(255,0,0);
    etc.

    currLinePtr:=currLinePtr^.next;     {point to next line}

    OR let's make all of the lines in the list blue for example;

    currLinePtr:=startLinePtr;
    while currLinePtr<>nil do
    begin
      currLinePtr^.colour:=rgb(0,0,255);
      currLinePtr:=currLinePtr^.next;     {point to next line}
    end;

5 - Finally kill the entire list from the heap

procedure destroyList;
var                                          
  nextPtr,
  currPtr: linePtr;
begin
  currPtr:=startLinePtr;
  while currPtr<>nil do
  begin
    nextPtr:=currPtr^.next;
    dispose(currPtr);
    currPtr:=nextPtr;
  end;
end;

6 - There's lots more you can add of course like an array of objects [1..10] each with there own
startPtr, currPtr and even an endPtr. You may even want to make it a doubly linked list so you
can go backwards and forwards.

Hope this sheds some light onto how pascal/Delphi does it.

I must point out that you can use TList or is it TstringList and add objects to it, but I've
always found this way to be useful and I reuse the code.

Cheers,
Andrew McLean
Aerosoft
Sydney, Australia

http://www.aerosoft.com.au
http://www.aerosoft.com.au/delphi
http://www.aerosoft.com.au/delphi/progman
http://www.aerosoft.com.au/hitcount

Re:Linked Lists in Delphi - how easy??


John M. Miano (visualw...@interramp.com) wrote:
: In article <823080578...@rhmtech.demon.co.uk>, roge...@rhmtech.demon.co.uk
: says...
: >
: >Excuse what is probably a simple query - can I construct linked
: >lists of records in the way I do in C using structures?
: >
It's even easier in Pascal.

: >If so, are 'new' and 'dispose' equivalent to C's 'malloc' and
: >'free' - if not, what would I use?  Or is there a better way??
:
Yes and yes.  As to a better way, that totally depends on your approach
and what you're trying to do.

: You are probably better off looking at a C++ Linked List example
: than a C one.  Try creating a linked list class rather than a
: record.  Use Create and Free to create the list.
:
While I agree with creating a Linked List class, I'm dubious as to the
value added for doing it.  If you're looking for a quick and dirty
solution to the problem, you can just go ahead and create a linked list
in one of your objects.

However, if you want an elegant *reusable* solution, go for creating the
class.

Todd Lang
lang1...@mach1.wlu.ca

Re:Linked Lists in Delphi - how easy??


In article <4erh7d$...@usenet2.interramp.com>, visualw...@interramp.com says...

Quote

>In article <823080578...@rhmtech.demon.co.uk>, roge...@rhmtech.demon.co.uk
>says...

>>Excuse what is probably a simple query - can I construct linked
>>lists of records in the way I do in C using structures?

>>If so, are 'new' and 'dispose' equivalent to C's 'malloc' and
>>'free' - if not, what would I use?  Or is there a better way??

>You are probably better off looking at a C++ Linked List example
>than a C one.  Try creating a linked list class rather than a
>record.  Use Create and Free to create the list.

>John

DON'T DO IT!!!!

Save yourself sometime and download some very nice structures units called
EZDSL.  You can get them off the Delphi Super page(I think thats were I got
them), if not email and I can email to you.  They are well written and
complete, author Julian M. Bucknall.  

I had done my own Link Lists, then after I found these, I went back and changed
 my code.  I don't think you will find a better set of OOP structure
objects(For Delphi) out there.

Some advise, take the time to print out the docs and source code.  READ the
docs, then try to use them.

Here is a bit of the docs:

                         Introduction
----------------------------------------------------------------------

The EZDSL units provide an OOP interface for classical data
structures for Delphi: stacks, queues, lists, binary trees and so
forth. For programmers migrating from BP7 a TCollection replacement
is also provided.

Re:Linked Lists in Delphi - how easy??


Hi Jon,

I have a complete link list library which has been working for about
10 months. If you want a copy then send me an email at
mbrin...@fl.net.au  and I will send it to you with source.

Mark

Re:Linked Lists in Delphi - how easy??


Quote
gr...@cryptocard.com (Greg Carter) wrote:
> In article <4erh7d$...@usenet2.interramp.com>, visualw...@interramp.com says...
> Save yourself sometime and download some very nice structures units called
> EZDSL.  You can get them off the Delphi Super page(I think thats were I got
> them), if not email and I can email to you.  They are well written and
> complete, author Julian M. Bucknall.  

EZDSL is pretty good, but has anyone done sets, dictionaries and other data
structures which are not found in EZDSL?

Have fun,
        Mazz.

m...@faceng.newcastle.edu.au

Other Threads