summaryrefslogtreecommitdiffstatshomepage
path: root/exercises/044_quiz5.zig
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-03-12 18:59:46 -0500
committerDave Gauer <dave@ratfactor.com>2021-03-12 18:59:46 -0500
commit0956f1839fcaaa273353148da9e157a8f9690d2f (patch)
treed6c90700131d5b28e898881f13e2a05612e4703f /exercises/044_quiz5.zig
parent93eefe0f250bb76bfdd8e6bb784b6a9586517000 (diff)
"999 is enough for anybody" triple-zero padding (#18)
When I hit 999 exercises, I will finally have reached the ultimate state of soteriological release and no more exercises will be needed. The cycle will be complete. All that will be left is perfect quietude, freedom, and highest happiness.
Diffstat (limited to 'exercises/044_quiz5.zig')
-rw-r--r--exercises/044_quiz5.zig47
1 files changed, 47 insertions, 0 deletions
diff --git a/exercises/044_quiz5.zig b/exercises/044_quiz5.zig
new file mode 100644
index 0000000..8a0d88c
--- /dev/null
+++ b/exercises/044_quiz5.zig
@@ -0,0 +1,47 @@
+//
+// "Elephants walking
+// Along the trails
+//
+// Are holding hands
+// By holding tails."
+//
+// from Holding Hands
+// by Lenore M. Link
+//
+const std = @import("std");
+
+const Elephant = struct {
+ letter: u8,
+ tail: *Elephant = undefined,
+ visited: bool = false,
+};
+
+pub fn main() void {
+ var elephantA = Elephant{ .letter = 'A' };
+ // (Please add Elephant B here!)
+ var elephantC = Elephant{ .letter = 'C' };
+
+ // Link the elephants so that each tail "points" to the next elephant.
+ // They make a circle: A->B->C->A...
+ elephantA.tail = &elephantB;
+ // (Please link Elephant B's tail to Elephant C here!)
+ elephantC.tail = &elephantA;
+
+ visitElephants(&elephantA);
+
+ std.debug.print("\n", .{});
+}
+
+// This function visits all elephants once, starting with the
+// first elephant and following the tails to the next elephant.
+// If we did not "mark" the elephants as visited (by setting
+// visited=true), then this would loop infinitely!
+fn visitElephants(first_elephant: *Elephant) void {
+ var e = first_elephant;
+
+ while (!e.visited) {
+ std.debug.print("Elephant {u}. ", .{e.letter});
+ e.visited = true;
+ e = e.tail;
+ }
+}