diff --git a/LList.zig b/LList.zig index 350aa08..78a99e8 100644 --- a/LList.zig +++ b/LList.zig @@ -1,28 +1,45 @@ const std = @import("std"); +const Allocator = std.mem.Allocator; -pub fn LList (comptime T: type) type { +pub fn LList(comptime T: type) type { return struct { const Self = @This(); first: ?*Node(T), + alloc: Allocator, - pub fn init(this: *Self) !void { // ! allocator: *std.mem.allocator als zweiter Parameter - this.first = null; + pub fn init(this: *Self, allocator: Allocator) !void { + this.alloc = allocator; + this.first = try allocator.create(Node(T)); } - pub fn deinit() void { + pub fn deinit(this: *Self) void { //TODO + this.alloc.destroy(T); //? Muss in einer Schleife durchlaufen + } + + pub fn add(this: *Self, value: T ) *type{ + const newFirst = try this.alloc.create(Node(T)); + + var node: Node(T) = undefined; + this.first.* = try node.init(value, this.first); + + this.first = newFirst; + return this.first; + + } }; } -pub fn Node (comptime T: type) type { +pub fn Node(comptime T: type) type { return struct { const Self = @This(); next: ?*Self, //? Nächste Node in der Liste + value: T, - - pub fn init(this: *Self, value: T) void { + pub fn init(this: *Self, value: T, next: ?*Self) void { this.value = value; + this.next = next; } pub fn printNode(this: *Self) !void { @@ -33,7 +50,10 @@ pub fn Node (comptime T: type) type { } pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = gpa.allocator(); var testList: LList(u64) = undefined; - try testList.init(); -} \ No newline at end of file + try testList.init(allocator); + +}