in reply to Doubly link list implementation
However, this is your code quickly modified to make a doubly-linkled list in the old fashion (I leave out the tail as an exercise):
This prints the values of the items in the list:#!/usr/bin/perl use strict; use warnings; my $head = {data => 0, prev => undef, next => undef}; my $previous = $head; while (<DATA>){ chomp; $previous->{next} = { "data" => $_ , "next" => undef, "prev" => $previous, }; $previous = $previous->{next}; } my $curr = $head; while (1) { last unless defined $curr->{next}; print "Current: ", $curr->{data}, "\tPrevious: ", $curr->{prev}{data} // "", "\tNext: ", $curr->{next}{data} // "", "\n"; $curr = $curr->{next}; } __DATA__ 1 2 3 4 5
And this is a Data Dumper view of the structure:$ perl doubly_linked.pl Current: 0 Previous: Next: 1 Current: 1 Previous: 0 Next: 2 Current: 2 Previous: 1 Next: 3 Current: 3 Previous: 2 Next: 4 Current: 4 Previous: 3 Next: 5
$VAR1 = { 'next' => { 'next' => { 'next' => { 'next' => { 'next' => { +'next' => undef, +'prev' => $VAR1->{'next'}{'next'}{'next'}{'next'}, +'data' => '5' }, 'prev' => $V +AR1->{'next'}{'next'}{'next'}, 'data' => '4 +' }, 'prev' => $VAR1->{'next' +}{'next'}, 'data' => '3' }, 'prev' => $VAR1->{'next'}, 'data' => '2' }, 'prev' => $VAR1, 'data' => '1' }, 'prev' => undef, 'data' => 0 };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Doubly link list implementation
by punitpawar (Sexton) on Nov 25, 2015 at 02:26 UTC | |
by punitpawar (Sexton) on Nov 25, 2015 at 19:04 UTC | |
by Laurent_R (Canon) on Nov 26, 2015 at 10:56 UTC | |
by punitpawar (Sexton) on Nov 25, 2015 at 23:13 UTC | |
|
Re^2: Doubly link list implementation
by punitpawar (Sexton) on Nov 25, 2015 at 02:26 UTC |