2024-11-06 15:44:28 +01:00
|
|
|
const std = @import("std");
|
2024-11-09 01:13:32 +01:00
|
|
|
const Allocator = std.mem.Allocator;
|
2024-11-06 15:44:28 +01:00
|
|
|
|
2024-11-09 01:13:32 +01:00
|
|
|
pub fn LList(comptime T: type) type {
|
2024-11-06 15:44:28 +01:00
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
2024-11-08 20:26:58 +01:00
|
|
|
first: ?*Node(T),
|
2024-11-09 01:13:32 +01:00
|
|
|
alloc: Allocator,
|
2024-11-08 20:26:58 +01:00
|
|
|
|
2024-11-09 01:13:32 +01:00
|
|
|
pub fn init(this: *Self, allocator: Allocator) !void {
|
|
|
|
this.alloc = allocator;
|
|
|
|
this.first = try allocator.create(Node(T));
|
2024-11-06 15:44:28 +01:00
|
|
|
}
|
|
|
|
|
2024-11-09 01:13:32 +01:00
|
|
|
pub fn deinit(this: *Self) void {
|
2024-11-06 15:46:19 +01:00
|
|
|
//TODO
|
2024-11-09 01:13:32 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2024-11-06 15:44:28 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-11-09 01:13:32 +01:00
|
|
|
pub fn Node(comptime T: type) type {
|
2024-11-06 15:44:28 +01:00
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
2024-11-08 21:03:04 +01:00
|
|
|
next: ?*Self, //? Nächste Node in der Liste
|
2024-11-09 01:13:32 +01:00
|
|
|
value: T,
|
2024-11-06 15:44:28 +01:00
|
|
|
|
2024-11-09 01:13:32 +01:00
|
|
|
pub fn init(this: *Self, value: T, next: ?*Self) void {
|
2024-11-06 16:07:42 +01:00
|
|
|
this.value = value;
|
2024-11-09 01:13:32 +01:00
|
|
|
this.next = next;
|
2024-11-06 16:07:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn printNode(this: *Self) !void {
|
|
|
|
const writer = std.io.getStdOut().writer();
|
|
|
|
try writer.print("Value: {any} \n", .{this.value});
|
2024-11-06 15:44:28 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() !void {
|
2024-11-09 01:13:32 +01:00
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
|
|
const allocator = gpa.allocator();
|
2024-11-08 21:03:04 +01:00
|
|
|
|
|
|
|
var testList: LList(u64) = undefined;
|
2024-11-09 01:13:32 +01:00
|
|
|
try testList.init(allocator);
|
|
|
|
|
|
|
|
}
|