Linked lists, Zydeco example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package SLL { | |
use Zydeco; | |
class Node { | |
has value ( type => Int ); | |
has nextnode ( type => Maybe[Object] ); | |
method printvalue () { | |
print $self->value, " "; | |
} | |
method go_next = $self->nextnode; | |
factory new_node ( Int $value, Object $next? ) { | |
return $class->new( | |
value => $value, | |
nextnode => $next | |
); | |
} | |
} | |
} | |
my $node_a = SLL->new_node(30); | |
my $node_b = SLL->new_node(40, $node_a); | |
my $node_c = SLL->new_node(70, $node_b); | |
my $node_head = SLL->new_node(0, $node_c); | |
my $node = $node_head; | |
while ( $node = $node->go_next ) { | |
$node->printvalue; | |
} | |
print "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment