Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Linked List

by Anonymous Monk
on Feb 24, 2000 at 01:37 UTC ( #3961=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to be able to write a PERL Script that contains a function that creates a node for a linked list.

Comment on Linked List
Re: Linked List
by stephen (Priest) on Feb 24, 2000 at 03:47 UTC
    What you'll want to do is read Mastering Algorithms With Perl by Jon Orwant et al. Then indeed you'll be able to do what you want to do.

    At the risk of giving away information too easily (and thus reducing the likelihood of enlightenment), here's an untested, off-the-top-of-my-head example of doing linked lists in Perl:

    A linked list node is just a data structure with one slot for data and one slot pointing to another node. So you could use an anonymous array, like so:

    ## ## make_node() ## Arguments: $value: scalar, contents of the node ## $next_node: linked list node (optional) ## sub new_node { my ($value, $next_node) = @_; my $new_node = [$value, $next_node]; return $new_node; }

    Okay, okay, so that would build the node backwards, but hey. Then, you could build an entire list by doing this:

    $last_node = new_node('last', undef); $middle_node = new_node('middle', $last_node); $first_node = new_node('first', $middle_node);

    You could traverse this list like so:

    sub traverse_nodes { my ($node) = @_; my ($value, $next_node) = @$node; if ( defined($next_node) ) { return ($value, traverse_nodes($next_node)); } else { return $value; } }

    At least I think that'll work. Somebody speak up if I'm smoking something. Then:

    print join(' ', traverse_nodes($first_node) ), "\n";
    should print "first, middle, last".

    stephen

Re: Linked List
by Anonymous Monk on Feb 24, 2000 at 06:49 UTC
    I once wrote linked-list and cons-cell classes in Perl, but I can't seem to find them anymore. Anyway, IIRC, I used a listref to hold the cons cells themselves and a recursive linked-list constructor (based on the classic (Lisp) definition of a (linked) list as either the empty list (undef) or the cons of a (scalar) value and a (possibly empty) list).
Code test
by Anonymous Monk on Mar 04, 2000 at 03:29 UTC
    Sorry, just learning usage of the code tag
    this is a lone of code this should be another line
Code test
by Anonymous Monk on Mar 04, 2000 at 03:29 UTC
    Sorry, just learning usage of the code tag. We need a preview submissions feature
    this is a line of code this should be another line
      While learning, why not creat a user and login?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://3961]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2014-12-19 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (78 votes), past polls