31 lines
928 B
Plaintext
31 lines
928 B
Plaintext
import std::io;
|
|||
|
|
struct counter { var count: int; }
|
||
|
|
struct node { var value: int; var next: *node; }
|
||
|
|
struct holder { val target: &mut counter; }
|
||
|
|
def bump(c: &mut counter): void { c.count += 1; }
|
||
|
|
def keep(c: &mut counter): holder { return holder{c}; }
|
||
|
|
def length(list: *node): int {
|
||
|
|
var count := 0;
|
||
|
|
var at := list;
|
||
|
|
while at != null { count++; at = (*at).next; }
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
def make(given: &mut counter): holder {
|
||
|
|
bump(given);
|
||
|
|
return keep(given);
|
||
|
|
}
|
||
|
|
def main(): void {
|
||
|
|
var n := counter{1};
|
||
|
|
bump(&mut n);
|
||
|
|
var made := counter{40};
|
||
|
|
val h := make(&mut made);
|
||
|
|
h.target.count += 1;
|
||
|
|
var third := node{3, null};
|
||
|
|
var second := node{2, &third as *node};
|
||
|
|
val first := node{1, &second as *node};
|
||
|
|
third.value = 30;
|
||
|
|
val ref: &counter = &n;
|
||
|
|
val alias := &ref;
|
||
|
|
n.count++;
|
||
|
|
println($"{n.count} {h.target.count} {length(&first as *node)} {(*(*first.next).next).value} {ref.count == 3} {alias.count} {h}");
|
||
|
|
}
|