www.destructor.de

About | Contact | Impressum


Home |  Code |  Articles |  Misc |  x
XML Parser |  TAR Library |  Linked Lists |  WinSock 1.1 |  x
General |  Usage |  Download |  Documentation |  Why? |  x

LinkList Usage

Prerequisite

Add the LibLinkList unit to your USES clause.


Nodes

Declare node object class as descendant of TListNode:

TYPE
  TMyNode = CLASS (TListNode)
              field1 : STRING [50];
              field2 : INTEGER;
              ...
            END;

Create list instance

Create an instance of your list:

VAR
  MyList : TLinkList;
BEGIN
  ...
  MyList := TLinkList.Create;
  ...

Append nodes

MyNode := TMyNode.Create (...);
MyList.Append (MyNode);

Insert Nodes

MyList.InsertFirst (MyNode);    // Inserts MyNode as first node of list
MyList.Insert (Node, MyNode);   // Inserts MyNode after Node

Delete Nodes

MyList.Delete (MyNode);

Scanning a List

Declare scan pointer variable:

VAR
  CurNode : TMyNode;

Do scanning:

MyList.StartScan (CurNode);
WHILE MyList.Scan (CurNode) DO BEGIN
  CurNode.DoSomething;  
  ...
  END;

NOTE: You must not delete the current node. If you want to delete nodes while scanning through the list, you need to hop from node to node for yourself.


Show Number of nodes in list

LblCount.Caption := IntToStr (MyList.Count);

Delete the second node from the list:

VAR
  NodeToDelete : TMyNode;
BEGIN
  ...
  NodeToDelete := MyList.At (1);
      // Node "1" is the second node because "At" expects the argument to be a zero-based index
  ...
  MyList.Delete (NodeToDelete);

Extracts NodeToDelete from the list and destroys it.
Use Extract instead of Delete if you don't want to destroy the node object.


Destroy the list object

MyList.Free;

This will also delete and destroy all remaining nodes from the list.