| 1 | # A node of a linked list. | ||||||||||
| 2 | |||||||||||
| 3 | package DataStructure::LinkedList::Node; | ||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use feature ':5.24'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use feature 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use Scalar::Util qw(weaken); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | =pod | ||||||||||
| 15 | |||||||||||
| 16 | =head1 NAME | ||||||||||
| 17 | |||||||||||
| 18 | DataStructure::LinkedList::Node | ||||||||||
| 19 | |||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||
| 21 | |||||||||||
| 22 | A single node (element) in a L<DataStructure::LinkedList>. | ||||||||||
| 23 | |||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||
| 25 | |||||||||||
| 26 | =head2 CONSTRUCTOR | ||||||||||
| 27 | |||||||||||
| 28 | You can’t build a node directly. Instead you can call one of the accessors of a | ||||||||||
| 29 | L<DataStructure::LinkedList> or some of the methods below. | ||||||||||
| 30 | |||||||||||
| 31 | Note that a B<DataStructure::LinkedList::Node> does not hold a reference on its | ||||||||||
| 32 | parent list. So a node becomes invalid when the last reference to its list is | ||||||||||
| 33 | deleted as the list itself will be destroyed. But you should also not depend on | ||||||||||
| 34 | this behavior as it might be fixed in the future. | ||||||||||
| 35 | |||||||||||
| 36 | =cut | ||||||||||
| 37 | |||||||||||
| 38 | # The constructor is private and should be called only by this package and its | ||||||||||
| 39 | # parent. | ||||||||||
| 40 | 12 | ▶ | sub new ($class, $list, $next, $value) { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 41 | 12 | my $self = bless { | |||||||||
| 42 | list => $list, | ||||||||||
| 43 | next => $next, | ||||||||||
| 44 | value => $value, | ||||||||||
| 45 | }, $class; | ||||||||||
| 46 | 12 | weaken($self->{list}); | |||||||||
| 47 | 12 | return $self; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | =pod | ||||||||||
| 51 | |||||||||||
| 52 | =head2 METHODS | ||||||||||
| 53 | |||||||||||
| 54 | All the functions below are class methods that should be called on a | ||||||||||
| 55 | C<DataStructure::LinkedList::Node> object. | ||||||||||
| 56 | |||||||||||
| 57 | =over 4 | ||||||||||
| 58 | |||||||||||
| 59 | =item value() | ||||||||||
| 60 | |||||||||||
| 61 | Returns the value held by this node. | ||||||||||
| 62 | |||||||||||
| 63 | =cut | ||||||||||
| 64 | |||||||||||
| 65 | 28 | ▶ | sub value ($self) { | ||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 66 | 28 | return $self->{value}; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | =pod | ||||||||||
| 70 | |||||||||||
| 71 | =item next() | ||||||||||
| 72 | |||||||||||
| 73 | Returns the next B<DataStructure::DoubleList::Node> in this list or B<undef> | ||||||||||
| 74 | if the current object is the last node in its list. | ||||||||||
| 75 | |||||||||||
| 76 | The current node can still be used after that call. | ||||||||||
| 77 | |||||||||||
| 78 | =cut | ||||||||||
| 79 | |||||||||||
| 80 | 34 | ▶ | sub next ($self) { | ||||||||
|
Subroutine
next: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 81 | 34 | return $self->{next}; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | =pod | ||||||||||
| 85 | |||||||||||
| 86 | =item insert_after($value) | ||||||||||
| 87 | |||||||||||
| 88 | Inserts a new node in the list after the current one, with the given value and | ||||||||||
| 89 | returns that new node. | ||||||||||
| 90 | |||||||||||
| 91 | The current node can still be used after that call. | ||||||||||
| 92 | |||||||||||
| 93 | =back | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub insert_after ($self, $value) { | ||||||||
|
Subroutine
insert_after: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 98 | 0 | my $new_node = new(ref $self, $self->{list}, $self->{next}, $value); | |||||||||
| 99 | 0 | $self->{next} = $new_node; | |||||||||
| 100 | 0 | ▶ | $self->{list}{last} = $new_node unless defined $new_node->{next}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | $self->{list}{size}++; | |||||||||
| 102 | 0 | return $new_node; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | # Delete the node, assuming that it is the first node of the list. | ||||||||||
| 106 | 8 | ▶ | sub _delete_first ($self) { | ||||||||
|
Subroutine
_delete_first: called CC=1 SCAR=0.0
| |||||||||||
| 107 | 8 | my $value = $self->{value}; | |||||||||
| 108 | 8 | $self->{list}{size}--; | |||||||||
| 109 | 8 | undef %{$self}; | |||||||||
| 110 | 8 | return $value; | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | 1 | 1; | |||||||||