Hi punitpawar,

It seems that you've found a solution by now, but if you really want to build a doubly-linked list, you should most probably create separate subs to insert new nodes (and also subs to delete nodes).

Subs to insert nodes could go like this:

sub insert_after { my ($previous, $val) = @_; my $curr_next = $previous->{next}; $previous->{next} = { "data" => $val , "next" => $curr_next, "prev" => $previous, }; my $new_next = $previous->{next}; $curr_next->{prev} = $new_next if defined $curr_next; return $new_next; } sub insert_before { # simply call insert_after on the previous node my ($previous, $val) = @_; return insert_after($previous->{prev}, $val); }
While we are at it, I think we should also move the displaying of the content into a separate sub:
sub display { my $curr = shift; while (1) { last unless defined $curr->{next}; print "Current: ", $curr->{data}, "\tPrevious: ", $curr->{prev}{data} // "", "\tNext: ", $curr->{next}{data} // "", "\n"; $curr = $curr->{next}; } print "\n"; }
I now change the program of my previous post to add two nodes after the original construction of the data structure, and display the structure before and after these insertions:
my $head = {data => 0, prev => undef, next => undef}; my $previous = $head; while (<DATA>){ # ... same as in previous post } display($head); my $new_node = insert_after ($head, 0.5); $new_node = insert_before ($new_node->{next}, 0.7); display($head);
This prints out the following:
$ 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 Current: 0 Previous: Next: 0.5 Current: 0.5 Previous: 0 Next: 0.7 Current: 0.7 Previous: 0.5 Next: 1 Current: 1 Previous: 0.7 Next: 2 Current: 2 Previous: 1 Next: 3 Current: 3 Previous: 2 Next: 4 Current: 4 Previous: 3 Next: 5
But, there is more to it.

Now that I have created subs to insert nodes, I can use them to build the original structure, rather than doing it manually in the while loop:
my $head = {data => 0, prev => undef, next => undef}; my $tail = $head; while (<DATA>){ chomp; $tail = insert_after($tail, $_); }
As per your request, I also add the insertion of a node just before the tail, and the overall program (without the subs already listed above) now looks like this:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $head = {data => 0, prev => undef, next => undef}; my $tail = $head; while (<DATA>){ chomp; $tail = insert_after($tail, $_); # new tail returned each time thr +ough } display($head); my $new_node = insert_after ($head, 0.5); $new_node = insert_before ($new_node->{next}, 0.7); # adding a new node before the tail $new_node = insert_before ($tail, 4.5); display($head);
Which duly prints:
$ 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 Current: 0 Previous: Next: 0.5 Current: 0.5 Previous: 0 Next: 0.7 Current: 0.7 Previous: 0.5 Next: 1 Current: 1 Previous: 0.7 Next: 2 Current: 2 Previous: 1 Next: 3 Current: 3 Previous: 2 Next: 4 Current: 4 Previous: 3 Next: 4.5 Current: 4.5 Previous: 4 Next: 5
This is just a quick illustration, there may be some edge cases not handled correctly (such as trying to add a node before the head, etc.). And I'll leave it to you to create the subs to remove nodes from the linked list, I guess it should be more or less obvious how to do it by now in general, you'll just have to get the details right.

I hope this helps.

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