|
@@ -10,38 +10,41 @@
|
|
//
|
|
//
|
|
const std = @import("std");
|
|
const std = @import("std");
|
|
|
|
|
|
-const Elephant = struct {
|
|
|
|
- letter: u8,
|
|
|
|
- tail: *Elephant = undefined,
|
|
|
|
- visited: bool = false,
|
|
|
|
|
|
+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", .{});
|
|
|
|
|
|
+pub fn main() void{
|
|
|
|
+ var elephantA = Elephant{ .letter = 'A' };
|
|
|
|
+ // (Please add Elephant B here!)
|
|
|
|
+ var elephantB = Elephant{ .letter = 'B' };
|
|
|
|
+ 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!)
|
|
|
|
+ elephantB.tail = &elephantC;
|
|
|
|
+ elephantC.tail = &elephantA;
|
|
|
|
+
|
|
|
|
+ visitElephants(&elephantA);
|
|
|
|
+
|
|
|
|
+ std.debug.print("\n", .{});
|
|
}
|
|
}
|
|
|
|
|
|
// This function visits all elephants once, starting with the
|
|
// This function visits all elephants once, starting with the
|
|
// first elephant and following the tails to the next elephant.
|
|
// first elephant and following the tails to the next elephant.
|
|
// If we did not "mark" the elephants as visited (by setting
|
|
// If we did not "mark" the elephants as visited (by setting
|
|
// visited=true), then this would loop infinitely!
|
|
// 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;
|
|
|
|
- }
|
|
|
|
|
|
+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;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|