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 Documentation

This is a Delphi CLASS which encapsulates all attributes and services needed to build, destroy and manage doubly linked lists of TListNode descendants.

FStartNode : TListNode;

Pointer to first node of the list.
NIL when list is empty.


FEndNode : TListNode;

Pointer to last node of the list. NIL when list is empty.


FCount : LONGINT;

Number of nodes in the list. Increased with every "Insert" operation. Decreased with every "Extract" or "Delete" operation.


CONSTRUCTOR Create;

Constructor for TLinkList. Initializes all status variables.


DESTRUCTOR  Destroy;

Destructor for TLinkList. Destroys all remaining nodes before destroying itself.


FUNCTION IsEmpty : BOOLEAN;

Returns TRUE when the list is empty.


FUNCTION CountNodes : LONGINT;

Counts the number of nodes in the list. Usually access to the "Count" property is much faster. You can use
"CountNodes" when you manipulated the list without using TLinkList methods.


PROPERTY First : TListNode READ FStartNode;

Returns a pointer to the first node in the list. Returns NIL when the list is empty.


PROPERTY Last : TListNode READ FEndNode;

Returns a pointer to the last node in the list. Returns NIL when the list is empty.


PROPERTY Count : LONGINT   READ FCount;

Returns the number of nodes in the list.
This count is only correct if you only used TLinkList services (methods) to add/delete nodes.


FUNCTION  At (Index0 : LONGINT)   : TListNode;

Returns the Index0-th node of the list. Index0 is zero-based, so passing "1" as Index0 returns the second node.
Returns NIL when Index0 is less than zero or larger than the number of nodes minus one.
This function scans through the list. This can take a bit of time if you have a very large list.


FUNCTION  IndexOf (Node : TListNode) : LONGINT;

Returns the zero-based index of the passed node.
This function scans through the list. This can take a bit of time if you have a very large list.
Returns -1 when "Node" can not be found in the list.


PROCEDURE Append (Node : TListNode);

Appends the passed Node at the end of the list (by calling Insert with InsertAfterNode=Last)


PROCEDURE Insert (InsertAfterNode, Node : TListNode);

Inserts the node into the list after the node "InsertAfterNode".
When "InsertAfterNode" is NIL, the node will become the first of the list.


PROCEDURE InsertFirst (Node : TListNode);

Appends the passed Node at the beginning of the list (by calling Insert with InsertAfterNode=NIL)


PROCEDURE InsertList  (InsertAfterNode : TListNode; TheList : TLinkList);

Inserts all nodes of list "TheList" after "InsertAfterNode". "TheList" is empty after this call.
Pass NIL for InsertAfterNode when you want the nodes of TheList to become the first nodes of the list.


PROCEDURE Extract (Node : TListNode);

Extracts node "Node" from the list. "Node" is not destroyed! Its Next and Prev pointers are initialized to NIL


PROCEDURE Delete (Node : TListNode);

Extracts node "Node" from the list and destroys it.


PROCEDURE Clear;

Clears the list by calling "Delete" for all nodes. So all nodes are destroyed.


PROCEDURE Ensort  (Node : TListNode);

Ensorts "Node" into the list. "Compare" is called to determine the position of the new node in the list


FUNCTION  Compare (Node1, Node2: TListNode) : INTEGER;  VIRTUAL; ABSTRACT;

Abstract Virtual function for comparisons needed by Ensort.
You MUST override this function if you want to use Ensort.
Compares nodes Node1 and Node2 for their order in the list.
Function result:

	< 0    if  Node1 < Node2
	= 0    if  Node1 = Node2
	> 0    if  Node1 > Node2 

Example

Your node objects (of type TPerson) contain a field "Age" (of type INTEGER) that you want the list to be sorted by.

	FUNCTION Compare (Node1, Node2 : TListNode) : INTEGER; OVERRIDE;
	BEGIN
	  Compare := TPerson (Node1).Age - TPerson (Node2).Age;
	END;

Ensorting the next person to the list:

	Person := TPerson.Create ('Stefan', 29);
	PersonList.Ensort (Person);

PROCEDURE StartScan (VAR ScanNode);
FUNCTION  Scan (VAR ScanNode) : BOOLEAN;

You can use these two functions to scan iteratively through the list. "ScanNode" is a variable of your node object. While scanning through the list, ScanNode points to the current node of the scan.

Example:

	VAR
	  CurrentPerson : TPerson;
	BEGIN
	  PersonList.StartScan (CurrentPerson);
	  WHILE PersonList.Scan (CurrentPerson) DO BEGIN
	    ShowMessage (CurrentPerson.Name + ' ' + IntToStr (CurrentPerson.Age));
	    END;
	END;