This was a useful exercise in Pascal or C to learn how to use pointers. Helped with conceptualizing them, and also taught a thing or three about how to turn a description into an algorithm, handling end cases and always being aware of the actual contents of a variable, which are useful in programming in general.

But if I really need to keep track of what proceeds and what follows, I choose to avoid references/pointers as needlessly complex.

use strict; use warnings; my @list; # Creating a doubly linked list while (<DATA>){ 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

Output:

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
Dum Spiro Spero

In reply to Re: Doubly link list implementation by GotToBTru
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.