I'm trying to make my own implementation of singly linked list, but getting an error while trying to initialize new node object via init method:
const std = @import("std");
pub fn SingleLinkedList(comptime T: type, allocator: std.mem.Allocator) type {
return struct {
head_node: ?*Node,
// List node
pub const Node = struct {
value: T,
next: ?*Node,
fn init(value: T) Node {
return Node{ .value = value, .next = null };
}
};
// List methods
pub fn init() @This() {
return .{ .head_node = null };
}
pub fn append(self: *@This(), value: T) !void {
// Init new node
var new_node = try allocator.create(Node);
new_node.init(value);
// If list doesn't have any nodes
if (self.head_node == null) {
self.head_node = new_node;
return;
}
// Appending new node to existing one
var current_node = self.head_node.?;
while (current_node.next) |node| {
current_node = node;
}
}
};
}