www.destructor.de
This is a Delphi CLASS which encapsulates all attributes and services needed to build, destroy and manage doubly linked lists of TListNode descendants.
Pointer to first node of the list.
NIL when list is empty.
Pointer to last node of the list. NIL when list is empty.
Number of nodes in the list. Increased with every "Insert" operation. Decreased with every "Extract" or "Delete" operation.
Constructor for TLinkList. Initializes all status variables.
Destructor for TLinkList. Destroys all remaining nodes before destroying itself.
Returns TRUE when the list is empty.
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.
Returns a pointer to the first node in the list. Returns NIL when the list is empty.
Returns a pointer to the last node in the list. Returns NIL when the list is empty.
Returns the number of nodes in the list.
This count is only correct if you only used TLinkList services (methods) to
add/delete nodes.
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.
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.
Appends the passed Node at the end of the list (by calling Insert with InsertAfterNode=Last)
Inserts the node into the list after the node "InsertAfterNode".
When "InsertAfterNode" is NIL, the node will become the first of the
list.
Appends the passed Node at the beginning of the list (by calling Insert with InsertAfterNode=NIL)
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.
Extracts node "Node" from the list. "Node" is not destroyed! Its Next and Prev pointers are initialized to NIL
Extracts node "Node" from the list and destroys it.
Clears the list by calling "Delete" for all nodes. So all nodes are destroyed.
Ensorts "Node" into the list. "Compare" is called to determine the position of the new node in the list
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);
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;