Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Struct

Node

Public Struct
std.SinglyLinkedList.Node
pub const Node = struct

This struct contains only a next pointer and not any data payload. The intended usage is to embed it intrusively into another data structure and access the data with @fieldParentPtr.

Fields

next: ?*Node = null

Functions

pub fn insertAfter(node: *Node, new_node: *Node) void
pub fn removeNext(node: *Node) ?*Node

Remove the node after the one provided, returning it.

pub fn findLast(node: *Node) *Node

Iterate over the singly-linked list from this node, until the final node is found.

This operation is O(N). Instead of calling this function, consider using a different data structure.

pub fn countChildren(node: *const Node) usize

Iterate over each next node, returning the count of all nodes except the starting one.

This operation is O(N). Instead of calling this function, consider using a different data structure.

pub fn reverse(indirect: *?*Node) void

Reverse the list starting from this node in-place.

This operation is O(N). Instead of calling this function, consider using a different data structure.

File

SinglyLinkedList.zig:25
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
pub const Node = struct {
    next: ?*Node = null,

    pub fn insertAfter(node: *Node, new_node: *Node) void {
        new_node.next = node.next;
        node.next = new_node;
    }

    /// Remove the node after the one provided, returning it.
    pub fn removeNext(node: *Node) ?*Node {
        const next_node = node.next orelse return null;
        node.next = next_node.next;
        return next_node;
    }

    /// Iterate over the singly-linked list from this node, until the final
    /// node is found.
    ///
    /// This operation is O(N). Instead of calling this function, consider
    /// using a different data structure.
    pub fn findLast(node: *Node) *Node {
        var it = node;
        while (true) {
            it = it.next orelse return it;
        }
    }

    /// Iterate over each next node, returning the count of all nodes except
    /// the starting one.
    ///
    /// This operation is O(N). Instead of calling this function, consider
    /// using a different data structure.
    pub fn countChildren(node: *const Node) usize {
        var count: usize = 0;
        var it: ?*const Node = node.next;
        while (it) |n| : (it = n.next) {
            count += 1;
        }
        return count;
    }

    /// Reverse the list starting from this node in-place.
    ///
    /// This operation is O(N). Instead of calling this function, consider
    /// using a different data structure.
    pub fn reverse(indirect: *?*Node) void {
        if (indirect.* == null) {
            return;
        }
        var current: *Node = indirect.*.?;
        while (current.next) |next| {
            current.next = next.next;
            next.next = indirect.*;
            indirect.* = next;
        }
    }
}