As aptly said by BrowserUk and other monks, it is almost never necessary to use linked lists in Perl. And, as illustrated by GotToBTru, a simple array can do it just as well in Perl (because it is so easy in Perl to add or remove items anywhere in the array).

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):

#!/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
This prints the values of the items in the list:
$ 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
And this is a Data Dumper view of the structure:
$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 };

In reply to Re: Doubly link list implementation by Laurent_R
in thread Doubly link list implementation by punitpawar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.