Ada linked lists
last modified November 23, 2026
A linked list stores its elements in separate nodes, each holding a value and an access value that designates the next node. The list itself only needs a pointer to the first node, and a second pointer to the last node makes appending cheap. This article builds a singly linked list step by step, moves it into a package, and then adds deletion, reversal in place, and ordered insertion.
Head and tail
Node is declared incomplete first so that Node_Ptr can
refer to it, and the full record comes after the access type. List
keeps a Head and a Tail; both start as
null. The program wires two nodes together by hand.
with Ada.Text_IO;
use Ada.Text_IO;
procedure First_List is
type Node;
type Node_Ptr is access Node;
type Node is record
Value : Integer;
Next : Node_Ptr;
end record;
type List is record
Head : Node_Ptr := null;
Tail : Node_Ptr := null;
end record;
L : List;
begin
L.Head := new Node'(10, null);
L.Tail := L.Head;
L.Tail.Next := new Node'(20, null);
L.Tail := L.Tail.Next;
Put_Line ("head =" & Integer'Image (L.Head.Value));
Put_Line ("tail =" & Integer'Image (L.Tail.Value));
end First_List;
After the first allocation, both Head and Tail point
at the same node. The second allocation is attached through
L.Tail.Next, and then Tail moves to the new node. The
first node still points to the second, so the chain is
10 -> 20 -> null.
$ gnatmake -gnat2022 -gnatwa first_list.adb $ ./first_list head = 10 tail = 20
Appending at the tail
Append allocates a node whose Next is null and attaches
it after the current tail. The empty list is a special case: both pointers must
be set to the new node. Print starts at the head and follows
Next until it reaches null.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Append_Demo is
type Node;
type Node_Ptr is access Node;
type Node is record
Value : Integer;
Next : Node_Ptr;
end record;
type List is record
Head : Node_Ptr := null;
Tail : Node_Ptr := null;
end record;
procedure Append (L : in out List; Value : Integer) is
New_Node : constant Node_Ptr := new Node'(Value, null);
begin
if L.Tail = null then
L.Head := New_Node;
else
L.Tail.Next := New_Node;
end if;
L.Tail := New_Node;
end Append;
procedure Print (L : List) is
P : Node_Ptr := L.Head;
begin
while P /= null loop
Put (Integer'Image (P.Value));
P := P.Next;
end loop;
New_Line;
end Print;
L : List;
begin
for I in 1 .. 4 loop
Append (L, I * 10);
end loop;
Print (L);
end Append_Demo;
Appending is constant time because the tail pointer is kept up to date; a list
with only a head pointer would have to walk to the end on every append.
New_Node is a constant access value that is never reassigned, and
the node itself becomes part of the chain. The output prints the values in
insertion order.
$ gnatmake -gnat2022 -gnatwa append_demo.adb $ ./append_demo 10 20 30 40
A list package
The package hides the node type in its private part, so clients see only
List and its operations. Clear walks the chain and
frees every node, which matters because a list allocates its nodes one at a
time. The specification and the body live in int_lists.ads and
int_lists.adb.
package Int_Lists is
type List is limited private;
procedure Append (L : in out List; Value : Integer);
procedure Prepend (L : in out List; Value : Integer);
procedure Print (L : List);
function Length (L : List) return Natural;
procedure Clear (L : in out List);
private
type Node;
type Node_Ptr is access Node;
type Node is record
Value : Integer;
Next : Node_Ptr;
end record;
type List is limited record
Head : Node_Ptr := null;
Tail : Node_Ptr := null;
end record;
end Int_Lists;
The private part is visible to the compiler but not to client code, so a client
cannot reach into a node or manufacture a List value by hand. The
operations keep the head and tail consistent, and Length counts by
traversal rather than storing a separate counter. The type is declared
limited private, so a client cannot copy a list with
L2 := L1; such a copy would duplicate the pointers and both copies
would later free the same nodes.
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
package body Int_Lists is
use Ada.Text_IO;
procedure Free is new Ada.Unchecked_Deallocation (Node, Node_Ptr);
procedure Append (L : in out List; Value : Integer) is
New_Node : constant Node_Ptr := new Node'(Value, null);
begin
if L.Tail = null then
L.Head := New_Node;
else
L.Tail.Next := New_Node;
end if;
L.Tail := New_Node;
end Append;
procedure Prepend (L : in out List; Value : Integer) is
New_Node : constant Node_Ptr := new Node'(Value, L.Head);
begin
L.Head := New_Node;
if L.Tail = null then
L.Tail := New_Node;
end if;
end Prepend;
procedure Print (L : List) is
P : Node_Ptr := L.Head;
begin
while P /= null loop
Put (Integer'Image (P.Value));
P := P.Next;
end loop;
New_Line;
end Print;
function Length (L : List) return Natural is
Count : Natural := 0;
P : Node_Ptr := L.Head;
begin
while P /= null loop
Count := Count + 1;
P := P.Next;
end loop;
return Count;
end Length;
procedure Clear (L : in out List) is
P : Node_Ptr := L.Head;
Next : Node_Ptr;
begin
while P /= null loop
Next := P.Next;
Free (P);
P := Next;
end loop;
L.Head := null;
L.Tail := null;
end Clear;
end Int_Lists;
Clear saves P.Next before freeing P,
because after the deallocation the node can no longer be read. Prepend
has the same empty-list special case as Append, but it links the
new node in front of the old head and touches the tail only when the list was
empty.
with Ada.Text_IO;
with Int_Lists;
use Ada.Text_IO;
procedure List_Main is
use Int_Lists;
L : List;
begin
Append (L, 10);
Append (L, 20);
Append (L, 30);
Prepend (L, 5);
Print (L);
Put_Line ("length =" & Integer'Image (Length (L)));
Clear (L);
Print (L);
Put_Line ("length =" & Integer'Image (Length (L)));
end List_Main;
The main program mixes both insertion operations and then clears the list.
Print after Clear outputs only a newline, and
Length returns zero. The list owns its nodes, so the client must
call Clear; a container that frees itself at the end of its scope
would use the Finalize hook from the controlled types article.
$ gnatmake -gnat2022 -gnatwa list_main.adb $ ./list_main 5 10 20 30 length = 4 length = 0
Deleting and reversing
Deletion needs the node before the one being removed, because that node must
skip over it. Delete walks with Prev and
Cur until it finds the first node with that value, then handles
three cases: the head, the tail, and a node in the middle. The specification
gains one line for Delete and one for Reverse_List;
the rest of the package is unchanged.
package Int_Lists is
type List is limited private;
procedure Append (L : in out List; Value : Integer);
procedure Prepend (L : in out List; Value : Integer);
procedure Delete (L : in out List; Value : Integer);
procedure Reverse_List (L : in out List);
procedure Print (L : List);
function Length (L : List) return Natural;
procedure Clear (L : in out List);
private
type Node;
type Node_Ptr is access Node;
type Node is record
Value : Integer;
Next : Node_Ptr;
end record;
type List is limited record
Head : Node_Ptr := null;
Tail : Node_Ptr := null;
end record;
end Int_Lists;
The procedure is called Reverse_List because reverse
is a reserved word in Ada and cannot be an identifier. Its body walks the chain
with three pointers: Prev is the already reversed prefix,
Cur is the node being processed, and Next saves the
rest of the list before the link is turned around.
procedure Delete (L : in out List; Value : Integer) is
Prev : Node_Ptr := null;
Cur : Node_Ptr := L.Head;
begin
while Cur /= null and then Cur.Value /= Value loop
Prev := Cur;
Cur := Cur.Next;
end loop;
if Cur = null then
return;
end if;
if Prev = null then
L.Head := Cur.Next;
else
Prev.Next := Cur.Next;
end if;
if Cur = L.Tail then
L.Tail := Prev;
end if;
Free (Cur);
end Delete;
procedure Reverse_List (L : in out List) is
Prev : Node_Ptr := null;
Cur : Node_Ptr := L.Head;
Next : Node_Ptr;
begin
L.Tail := L.Head;
while Cur /= null loop
Next := Cur.Next;
Cur.Next := Prev;
Prev := Cur;
Cur := Next;
end loop;
L.Head := Prev;
end Reverse_List;
If the value is absent, Delete reaches null and returns without
changing anything. Removing the head moves Head forward; removing
the tail moves Tail back to Prev, which is null when
the list had only one node. In Reverse_List, the old head becomes
the new tail before the links are rewritten, and the old tail becomes the new
head.
with Int_Lists;
procedure Delete_Main is
use Int_Lists;
L : List;
begin
for I in 1 .. 5 loop
Append (L, I * 10);
end loop;
Delete (L, 30);
Print (L);
Delete (L, 10);
Print (L);
Delete (L, 99);
Print (L);
Reverse_List (L);
Print (L);
Clear (L);
end Delete_Main;
The first deletion removes a node from the middle, the second removes the head,
and the third asks for a value that is not present and changes nothing. The
reverse then turns 20 40 50 into 50 40 20. Note that
delete_main.adb only needs Int_Lists, not
Ada.Text_IO, because all printing happens inside the package.
$ gnatmake -gnat2022 -gnatwa delete_main.adb $ ./delete_main 10 20 40 50 20 40 50 20 40 50 50 40 20
An applied example: a sorted list
Ordered insertion combines the previous operations: walk to the first node whose
value is not smaller than the new one, then insert before it. The specification
gains procedure Insert (L : in out List; Value : Integer);, and the
body uses Prepend for the front, Append for the end,
and a direct link in the middle.
procedure Insert (L : in out List; Value : Integer) is
Prev : Node_Ptr := null;
Cur : Node_Ptr := L.Head;
begin
while Cur /= null and then Cur.Value < Value loop
Prev := Cur;
Cur := Cur.Next;
end loop;
if Prev = null then
Prepend (L, Value);
elsif Cur = null then
Append (L, Value);
else
Prev.Next := new Node'(Value, Cur);
end if;
end Insert;
The loop stops at the first value that is greater than or equal to the new one,
so equal values are inserted before the existing node and the list stays
ordered. When Prev is null the new node belongs at the front; when
Cur is null it belongs at the back; otherwise it goes between the
two pointers.
with Int_Lists;
procedure Sorted_Main is
use Int_Lists;
type Values is array (Positive range <>) of Integer;
Data : constant Values := [30, 10, 50, 20, 40, 10];
L : List;
begin
for V of Data loop
Insert (L, V);
end loop;
Print (L);
Clear (L);
end Sorted_Main;
The values arrive in scrambled order and leave sorted. The duplicate 10 is kept, so the output has six numbers. Insertion is linear in the length of the list, which is the price of keeping the structure ordered without a tree or a hash table.
$ gnatmake -gnat2022 -gnatwa sorted_main.adb $ ./sorted_main 10 10 20 30 40 50
Next steps
The next article builds other structures from the same node pattern: a generic dynamic stack, a queue with head and tail pointers, and a binary search tree whose in-order traversal visits its values in sorted order. The access types article explains allocation and deallocation, and controlled types shows how a list can free itself automatically.
Source
In this article we have declared nodes that point to nodes, kept head and tail pointers, appended and prepended values, moved the operations into a package, deleted nodes from every position, reversed a list in place, and maintained a sorted list with ordered insertion. The packages article explains the specification and private part used here.
Author
List all Ada tutorials.