7R35C0/singly_linked_list
The module contains functions used in singly linked lists.
The module contains functions used in singly linked lists.
These functions do not make assertions, can lead to infinite loops in code and cycles in nodes or lists.
As a general rule, cycles should be broken before any other changes. Almost all functions are candidates for applying this rule. Keep this in mind when infinite loops occur in code and their causes are not very obvious.
Some important facts about cycles:
The code is based on following sources:
A singly linked list is a linear data structure that includes a series of connected nodes.
The Node
represents the type of nodes in a list
pub const Node = struct {
data: T,
next: ?*Node = null,
// some implementation code
};
where field:
data
is a value for the node itselfnext
is a pointer to the next nodeThe SinglyLinkedList
type has a single field
pub fn SinglyLinkedList(comptime T: type) type {
return struct {
head: ?*Node = null,
// some implementation code
};
}
where field head
is a pointer to the first node in list.
Above structures use an optional pointer ?*Node
, with default value null
.
The meaning of null
depends on context:
.head == null
.next == null
More information about cycles in nodes and singly linked list can be found in
IMPORTANT file. Also, more detailed examples are in the demo
files.