Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Linked List

by RichardK (Vicar)
on Sep 04, 2013 at 08:42 UTC ( #1052252=note: print w/ replies, xml ) Need Help??


in reply to Linked List

You could remove the need for the $pred variable altogether, if you work with a little more indirection. So something along these lines :-

while ( defined $curr ) { if ($curr->[NEXT]->[VALUE] == $value) { $curr->[NEXT] = $curr->[NEXT]->[NEXT]; } else { $curr = $curr->[NEXT]; }

You'll have to think about what happens at the start and end of your list.

The next thing to do is to write a sort by insertion routine ;)


Comment on Re: Linked List
Download Code
Re^2: Linked List
by code-ninja (Scribe) on Sep 04, 2013 at 09:02 UTC
    Working on it! Insertion sort and all.

    well, to be frank enough, I find Perl's syntax a bit more cryptic than C (though that might be because I've mostly programmed in C throughout my academic life). I'm using this book but it assumes that you are Perl samurai if not a ninja (or a Jedi) if you get what I mean. :/

    but whatever, I learn better (with a steep learning curve) by doing... so I'll soon be asking doubts about insertion sorting ;)
Re^2: Linked List
by choroba (Canon) on Sep 04, 2013 at 10:20 UTC
    Only the leftmost dereference arrow is mandatory:
    while ( defined $curr ) { if ($curr->[NEXT][VALUE] == $value) { $curr->[NEXT] = $curr->[NEXT][NEXT]; } else { $curr = $curr->[NEXT]; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2015-07-06 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls