Hello, I do have a follow up question regarding node insertion in doubly linked list. so my intent is to insert a node after the head node and before the last node.
Although I am able to do the insert correctly , when I try to print the output starting from the head . I do not see node that I had inserted before the tail node.
Similarly when I print the output starting from the tail node , I do not see node that I inserted after the head node.

Observe the values printed in the "current" field from my output.

Any corrections/ suggestions which you can provide will be very helpful

here is my code
#!/usr/bin/perl use strict; use warnings; 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 at the beginning of the list after the head node my $curr = $head; $curr->{next}= { "data" => 30, "next" => $curr->{next}, "prev" => $curr, }; ### Inserting element at the end of the list before the tail node $curr = $previous; $curr->{prev}= { "data" => 40, "next" => $curr, "prev" => $curr->{prev}, }; ## Printing from head print "printing from head \n"; $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 "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 form this program
printing from head Current: 0 Previous: Next: 30 Current: 30 Previous: 0 Next: line1 Current: line1 Previous: 0 Next: line2 Current: line2 Previous: line1 Next: line3 Current: line3 Previous: line2 Next: line4 Current: line4 Previous: line3 Next: line5 Printing From tail Current: line5 Previous: 40 Next: Current: 40 Previous: line4 Next: line5 Current: line4 Previous: line3 Next: line5 Current: line3 Previous: line2 Next: line4 Current: line2 Previous: line1 Next: line3 Current: line1 Previous: 0 Next: line2 Current: 0 Previous: Next: 30
content of datastored.txt
line1 line2 line3 line4 line5

In reply to Re^3: 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.