221V 2 days ago
parent
commit
ad58e59ac6
1 changed files with 40 additions and 38 deletions
  1. 40 38
      exercises/046_optionals2.zig

+ 40 - 38
exercises/046_optionals2.zig

@@ -20,52 +20,54 @@
 //
 //
 const std = @import("std");
 const std = @import("std");
 
 
-const Elephant = struct {
-    letter: u8,
-    tail: *Elephant = null, // Hmm... tail needs something...
-    visited: bool = false,
+const Elephant = struct{
+  letter: u8,
+  tail: ?*Elephant = null, // Hmm... tail needs something...
+  visited: bool = false,
 };
 };
 
 
-pub fn main() void {
-    var elephantA = Elephant{ .letter = 'A' };
-    var elephantB = Elephant{ .letter = 'B' };
-    var elephantC = Elephant{ .letter = 'C' };
-
-    // Link the elephants so that each tail "points" to the next.
-    linkElephants(&elephantA, &elephantB);
-    linkElephants(&elephantB, &elephantC);
-
-    // `linkElephants` will stop the program if you try and link an
-    // elephant that doesn't exist! Uncomment and see what happens.
-    // const missingElephant: ?*Elephant = null;
-    // linkElephants(&elephantC, missingElephant);
-
-    visitElephants(&elephantA);
-
-    std.debug.print("\n", .{});
+pub fn main() void{
+  var elephantA = Elephant{ .letter = 'A' };
+  var elephantB = Elephant{ .letter = 'B' };
+  var elephantC = Elephant{ .letter = 'C' };
+  
+  // Link the elephants so that each tail "points" to the next.
+  linkElephants(&elephantA, &elephantB);
+  linkElephants(&elephantB, &elephantC);
+  linkElephants(&elephantC, &elephantA);
+  
+  // `linkElephants` will stop the program if you try and link an
+  // elephant that doesn't exist! Uncomment and see what happens.
+  // const missingElephant: ?*Elephant = null;
+  // linkElephants(&elephantC, missingElephant);
+  
+  visitElephants(&elephantA);
+  
+  std.debug.print("\n", .{});
 }
 }
 
 
 // If e1 and e2 are valid pointers to elephants,
 // If e1 and e2 are valid pointers to elephants,
 // this function links the elephants so that e1's tail "points" to e2.
 // this function links the elephants so that e1's tail "points" to e2.
-fn linkElephants(e1: ?*Elephant, e2: ?*Elephant) void {
-    e1.?.tail = e2.?;
+fn linkElephants(e1: ?*Elephant, e2: ?*Elephant) void{
+  e1.?.tail = e2.?;
 }
 }
 
 
 // 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.
-fn visitElephants(first_elephant: *Elephant) void {
-    var e = first_elephant;
-
-    while (!e.visited) {
-        std.debug.print("Elephant {u}. ", .{e.letter});
-        e.visited = true;
-
-        // We should stop once we encounter a tail that
-        // does NOT point to another element. What can
-        // we put here to make that happen?
-
-        // HINT: We want something similar to what `.?` does,
-        // but instead of ending the program, we want to exit the loop...
-        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;
+    
+    // We should stop once we encounter a tail that
+    // does NOT point to another element. What can
+    // we put here to make that happen?
+    
+    // HINT: We want something similar to what `.?` does,
+    // but instead of ending the program, we want to exit the loop...
+    e = e.tail orelse break;
+  }
 }
 }
+