in reply to Re^2: Doubly link list implementation
in thread Doubly link list implementation
Observe the values printed in the "current" field from my output.
Any corrections/ suggestions which you can provide will be very helpfulOutput form this program#!/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}; }
content of datastored.txtprinting 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
line1 line2 line3 line4 line5
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Doubly link list implementation
by Laurent_R (Canon) on Nov 26, 2015 at 10:56 UTC | |
|
Re^4: Doubly link list implementation
by punitpawar (Sexton) on Nov 25, 2015 at 23:13 UTC |