login

Linked List

A linked list is a data structure which consists of a sequence of data records such that in each record there is a field that contains a reference to the next record in the sequence.

 #!/usr/bin/perl

 $list = undef;
 foreach (reverse 1..5) {

     $list = [ $list, $_ * $_ ];

 }
 

 use constant NEXT => 0;
 use constant VAL  => 1;

 $four    = $list->[NEXT];
 $nine    = $four->[NEXT];
 $sixteen = $nine->[NEXT];
 

 $nine->[NEXT]    = $sixteen->[NEXT];
 $sixteen->[NEXT] = $nine;
 $four->[NEXT]    = $sixteen;

#!/usr/bin/perl

$list = $tail = undef;

foreach (1..5) {

    my $node = [ undef, $_ * $_ ];

    $list = undef;
    $tail = \$list;

    foreach (1..5) {
    my $node = [ undef, $_ * $_ ];
    $$tail = $node;
    $tail = \$node->[NEXT];

    }
}