Ok I was able to get the solution. I was doing a dumb mistake.
I never created the link backward from the newly added node.
Again thanks everyone for your responses and help


See my revised code below .
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $head = {data => 0, prev => undef, next => undef}; my $previous = $head; open FILE, "<datastored.txt" or die $!; while (<FILE>){ chomp; $previous->{next} = { "data" => $_ , "next" => undef, "prev" => $previous, }; $previous = $previous->{next}; } ### Inserting element after the head node my $curr = $head; $curr->{next}= { "data" => 30, "next" => $curr->{next}, "prev" => $curr, }; $curr->{next}{next}{prev}=$curr->{next}; # # Inserting element before the tail node $curr = $previous; $curr->{prev}= { "data" => 40, "next" => $curr, "prev" => $curr->{prev}, }; $curr->{prev}{prev}{next}=$curr->{prev}; ## Printing from head print "printing from head \n"; print Dumper($head); $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}; } ## printing from tail print "\n Printing From tail \n"; while (1) { last unless defined $curr->{prev}; print "Current: ", $curr->{data}, "\tPrevious: ", $curr->{prev}{data} // "", "\tNext: ", $curr->{next}{data} // "", "\n"; $curr = $curr->{prev}; }
Output
punit:Desktop punitpawar$ perl test1.pl printing Dumper $VAR1 = { 'data' => 0, 'prev' => undef, 'next' => { 'prev' => $VAR1, 'next' => { 'next' => { 'data' => 'line 2', 'next' => { 'next' => { +'prev' => $VAR1->{'next'}{'next'}{'next'}{'next'}, +'next' => { + 'data' => 'line 4', + 'next' => undef, + 'prev' => $VAR1->{'next'}{'next'}{'next'}{'next'}{'next'} + }, +'data' => 40 }, 'prev' => $V +AR1->{'next'}{'next'}{'next'}, 'data' => 'l +ine 3' }, 'prev' => $VAR1->{'next' +}{'next'} }, 'prev' => $VAR1->{'next'}, 'data' => 'line 1' }, 'data' => 30 } }; printing from head Current: 0 Previous: Next: 30 Current: 30 Previous: 0 Next: line 1 Current: line 1 Previous: 30 Next: line 2 Current: line 2 Previous: line 1 Next: line 3 Current: line 3 Previous: line 2 Next: 40 Current: 40 Previous: line 3 Next: line 4 Printing From tail Current: line 4 Previous: 40 Next: Current: 40 Previous: line 3 Next: line 4 Current: line 3 Previous: line 2 Next: 40 Current: line 2 Previous: line 1 Next: line 3 Current: line 1 Previous: 30 Next: line 2 Current: 30 Previous: 0 Next: line 1 Current: 0 Previous: Next: 30
Content of datastored.txt
line1 line2 line3 line4

In reply to Re^4: Doubly link list implementation by punitpawar
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.