Anonymous user
Singly-linked list/Element removal: Difference between revisions
Singly-linked list/Element removal (view source)
Revision as of 22:20, 14 November 2018
, 5 years agoadd →{{header|Perl 6}}
(add →{{header|Perl 6}}) |
|||
Line 388:
After 2nd removal : 1
</pre>
=={{header|Perl 6}}==
Extending <tt>class Cell</tt> from [[Singly-linked_list/Element_definition#Perl_6]]:
<lang perl6> method delete ($value --> Cell) {
my $prev = Nil;
my $cell = self;
my $new-head = self;
while $cell {
my $next = $cell.next;
if $cell.value == $value {
$prev.next = $next if $prev;
$cell.next = Nil;
$new-head = $next if $cell === $new-head;
}
else {
$prev = $cell;
}
$cell = $next;
}
return $new-head;
}</lang>
Usage:
<lang perl6>my $list = cons 10, (cons 20, (cons 10, (cons 30, Nil)));
$list = $list.delete(10);</lang>
=={{header|Phix}}==
|