in reply to Re^3: Doubly link list implementation
in thread Doubly link list implementation

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.