0Pricing
Zig Academy · Lektion

Eine einfach verkettete Liste

Knoten, Besitz und Traversierung.

Eine einfach verkettete Liste ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

What a Linked List Is

A singly linked list chains values together: each node holds a value and a pointer to the next node, or null at the end. 🔗

Define the Node

A node is a struct with a value and an optional pointer to the next node. Optional means the last node points at null.

const Node = struct {
    value: i32,
    next: ?*Node,
};

Why the Pointer Is Optional

Using ?*Node lets the chain end safely. There is no need for a special sentinel value to mark the tail.

next: ?*Node

The List Tracks the Head

The list itself just remembers the first node, called the head, plus the allocator that owns every node.

const List = struct {
    head: ?*Node,
    alloc: std.mem.Allocator,
};

Allocate One Node

To add a value, ask the allocator to create a single Node on the heap. This returns a pointer to fresh storage.

const node = try self.alloc.create(Node);

Push to the Front

The cheapest insert is at the head: point the new node at the old head, then make it the new head. This is constant time.

node.* = .{ .value = v, .next = self.head };
self.head = node;

Walk the Chain

To traverse, start at head and follow each next pointer until you reach null. A while loop captures the node nicely.

var cur = self.head;
while (cur) |n| {
    std.debug.print("{d}\n", .{n.value});
    cur = n.next;
}

The Capture Unwraps the Optional

Writing while (cur) |n| runs the body only while cur is non-null, and n is the unwrapped pointer. The loop ends at null.

while (cur) |n| { ... }

You Own Every Node

Each node came from the allocator, so each must be returned. Forgetting even one is a leak the testing allocator will report.

Free the Whole List

Walk the chain in deinit, but grab next before freeing, because the node is gone the moment you destroy it.

var cur = self.head;
while (cur) |n| {
    const nxt = n.next;
    self.alloc.destroy(n);
    cur = nxt;
}

Make It Generic

Wrap Node and List in a type-returning function so the list works for any element type, not just i32.

fn LinkedList(comptime T: type) type {
    return struct {
        const Node = struct { value: T, next: ?*Node };
    };
}

Quick Check

You are freeing a linked list node by node. What must you do before destroying each node?

Recap

Nodes hold a value and an optional next pointer. The list tracks the head and an allocator, pushes to the front, and frees node by node. 🎯

Häufig gestellte Fragen

Ist die Lektion „Eine einfach verkettete Liste“ kostenlos?

Ja — der vollständige Text von „Eine einfach verkettete Liste“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Eine einfach verkettete Liste“?

Knoten, Besitz und Traversierung. Du übst Zig Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Zig Academy zu starten?

Keine Vorkenntnisse erforderlich. Zig Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Eine einfach verkettete Liste“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Zig Academy-Lektion Code schreiben und ausführen?

Ja. Jede Zig Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Ein generischer Stack von Grund auf
  2. Eine einfach verkettete Liste
  3. HashMap und AutoHashMap verwenden
  4. Profiling und Sicherheitsabwägungen
← Zurück zu Zig Academy