Compare commits
4 Commits
72ff38beb9
...
20945b8aee
Author | SHA1 | Date |
---|---|---|
2wenty1ne | 20945b8aee | |
Anastasia Kisner | 83f6a03ff5 | |
Anastasia Kisner | c118f0fc7d | |
Anastasia Kisner | 0c0d74c00e |
119
LList.zig
119
LList.zig
|
@ -1,37 +1,105 @@
|
|||
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),
|
||||
//TODO Liste mit allen Nodes erstellen
|
||||
first: ?*Node(T),
|
||||
alloc: Allocator,
|
||||
|
||||
pub fn init(this: *Self) !void {
|
||||
//TODO Heap mit allocater nutzen
|
||||
var node: Node(T) = undefined;
|
||||
node.init(69);
|
||||
try node.printNode();
|
||||
this.first = &node;
|
||||
|
||||
pub fn init(this: *Self, allocator: Allocator) !void {
|
||||
this.alloc = allocator;
|
||||
this.first = null;
|
||||
}
|
||||
|
||||
pub fn deinit() void {
|
||||
//TODO
|
||||
|
||||
pub fn deinit(this: *Self) !void {
|
||||
|
||||
//? If there is no created Node
|
||||
if (this.first == null) {
|
||||
//? Nothing to do
|
||||
return;
|
||||
}
|
||||
|
||||
var currentNode: *Node(T) = this.first orelse unreachable;
|
||||
|
||||
while (true) {
|
||||
const nextNode = currentNode.next orelse unreachable;
|
||||
var breakNext = false;
|
||||
|
||||
if (nextNode.next == null) {
|
||||
breakNext = true;
|
||||
}
|
||||
|
||||
try currentNode.deinit();
|
||||
|
||||
currentNode = nextNode;
|
||||
|
||||
if (breakNext == true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
try currentNode.deinit();
|
||||
}
|
||||
|
||||
|
||||
pub fn add(this: *Self, value: T ) !*Node(T){
|
||||
|
||||
const newNode: *Node(T) = try this.alloc.create(Node(T));
|
||||
try newNode.init(this.alloc, value, this.first);
|
||||
this.first = newNode;
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
|
||||
pub fn printList(this: *Self) !void {
|
||||
const writer = std.io.getStdOut().writer();
|
||||
|
||||
if (this.first == null) {
|
||||
try writer.print("List is empty \n", .{});
|
||||
return;
|
||||
}
|
||||
|
||||
var currentNode: *Node(T) = this.first orelse unreachable;
|
||||
|
||||
while (true) {
|
||||
try writer.print("{any}\n", .{currentNode.value.*});
|
||||
currentNode = currentNode.next orelse unreachable;
|
||||
|
||||
if (currentNode.next == null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
try writer.print("{any}\n", .{currentNode.value.*});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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
|
||||
alloc: Allocator,
|
||||
next: ?*Self, //? Nächste Node in der Liste
|
||||
value: *T,
|
||||
|
||||
position: i64, //? Position der Node in der List, später übergeben mit zB List.len
|
||||
value: T,
|
||||
pub fn init(this: *Self, allocator: Allocator, value: T, next: ?*Self) !void {
|
||||
this.alloc = allocator;
|
||||
this.next = next;
|
||||
|
||||
pub fn init(this: *Self, value: T) void {
|
||||
this.value = value;
|
||||
this.value = try this.alloc.create(T);
|
||||
this.value.* = value;
|
||||
}
|
||||
|
||||
|
||||
pub fn deinit(this: *Self) !void {
|
||||
this.alloc.destroy(this.value);
|
||||
this.alloc.destroy(this);
|
||||
}
|
||||
|
||||
|
||||
pub fn printNode(this: *Self) !void {
|
||||
const writer = std.io.getStdOut().writer();
|
||||
try writer.print("Value: {any} \n", .{this.value});
|
||||
|
@ -40,6 +108,17 @@ pub fn Node (comptime T: type) type {
|
|||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var testList: LLIst(u64) = undefined;
|
||||
try testList.init();
|
||||
}
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
var testList: LList(u64) = undefined;
|
||||
try testList.init(allocator);
|
||||
_ = try testList.add(1);
|
||||
_ = try testList.add(2);
|
||||
_ = try testList.add(3);
|
||||
_ = try testList.add(4);
|
||||
|
||||
try testList.printList();
|
||||
|
||||
try testList.deinit();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue