structstd.linked_list.SinglyLinkedList.Node[src]

Node inside the linked list wrapping the actual data.

Fields

next: ?*Node = null
data: T

Values

ConstantData[src]

Source Code

Source code
pub const Data = T

Functions

FunctioninsertAfter[src]

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

Insert a new node after the current one.

Arguments: new_node: Pointer to the new node to insert.

Parameters

node: *Node
new_node: *Node

Source Code

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

FunctionremoveNext[src]

pub fn removeNext(node: *Node) ?*Node

Remove a node from the list.

Arguments: node: Pointer to the node to be removed. Returns: node removed

Parameters

node: *Node

Source Code

Source code
pub fn removeNext(node: *Node) ?*Node {
    const next_node = node.next orelse return null;
    node.next = next_node.next;
    return next_node;
}

FunctionfindLast[src]

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).

Parameters

node: *Node

Source Code

Source code
pub fn findLast(node: *Node) *Node {
    var it = node;
    while (true) {
        it = it.next orelse return it;
    }
}

FunctioncountChildren[src]

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).

Parameters

node: *const Node

Source Code

Source code
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;
}

Functionreverse[src]

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

Reverse the list starting from this node in-place. This operation is O(N).

Parameters

indirect: *?*Node

Source Code

Source code
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;
    }
}

Source Code

Source code
pub const Node = struct {
    next: ?*Node = null,
    data: T,

    pub const Data = T;

    /// Insert a new node after the current one.
    ///
    /// Arguments:
    ///     new_node: Pointer to the new node to insert.
    pub fn insertAfter(node: *Node, new_node: *Node) void {
        new_node.next = node.next;
        node.next = new_node;
    }

    /// Remove a node from the list.
    ///
    /// Arguments:
    ///     node: Pointer to the node to be removed.
    /// Returns:
    ///     node removed
    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).
    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).
    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).
    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;
        }
    }
}