use strict; use warnings; my @list; # Creating a doubly linked list while (){ push @list, $_ } chomp @list; &print_list(); sub print_list { my $format = "%5s -> %5s -> %5s\n"; printf $format,'prev','data','next'; for (my $i=0; $i<$#list; $i++) { printf $format, $i>0 ? $list[$i-1] : 'undef', $list[$i], $i < $#list - 1 ? $list[$i+1] : 'undef' } } __DATA__ the quick brown fox jumps over the lazy dog #### prev -> data -> next undef -> the -> quick the -> quick -> brown quick -> brown -> fox brown -> fox -> jumps fox -> jumps -> over jumps -> over -> the over -> the -> lazy the -> lazy -> dog lazy -> dog -> undef