Singly-Linked List (element)
singly-linked list
Using this method, insert an element C into a list comprised of elements A->B, following element A.

See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


 
const std = @import("std");
 
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
 
const allocator = arena.allocator();
 
pub fn LinkedList(comptime Value: type) type {
    return struct {
        const This = @This();
 
        const Node = struct {
            value: Value,
            next: ?*Node,
        };
 
        head: ?*Node,
        tail: ?*Node,
 
        pub fn init() This {
            return LinkedList(Value) {
                .head = null,
                .tail = null,
            };
        }
 
        pub fn add(this: *This, value: Value) !void {
            var newNode = try allocator.create(Node);
 
            newNode.* = .{ .value = value, .next = null };
 
            if (this.tail) |tail| {
                tail.next = newNode;
                this.tail = newNode;
            } else if (this.head) |head| {
                head.next = newNode;
                this.tail = newNode;
            } else {
                this.head = newNode;
            }
        }
    };
}
 