Delphi - Linked Lists

Programs use several methods to store large amounts of related information

Typically, each element of a linked list stores a pointer to (address of) the next item in the list. Double linked lists also store a pointer to the previous item.

By convention, in the last element of the list, next contains zero (nil in Delphi).

Each element of the list is allocated on the heap.

Normally, data is stored in these locations - the first 3 are located in RAM (which provides fast access), the last 2 are not (and are slower to access).

Example based on TScreen.Cursors | Example from the Help | Other Variable Length Lists of Data


Example based on TScreen.Cursors

This example is from forms.pas, the comments are added by me.


Example from the Help

This example is from the help example for the New procedure - it adds one element to the head (beginning) of the list.


Other Variable Length Lists of Data

Delphi provides a number of list types so that you don't have to manage pointers to the next (and/or previous) objects. Instead, you just use an index, similar to an array.

Actually, the following are really just dynamic arrays - the allocated memory is always contiguous.

When either list needs to expand, a new contiguous block of memory is allocated on the heap, the existing data is copied to it, and the old block is deallocated.

Notice that the objects (and/or strings) are not stored in the list (just the pointers are stored) and are not contiguous in memory.

Though TMemo, TRichEdit, TComboBox, TListBox and the like access their data via a TStrings interface, they actually store their data using the Windows api (because they are actually wrappers for Windows Controls).


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / Delphi / LinkedLists.html