in reply to implementing linklist structure in perl
As Polonius, GrandFather and Joost mentioned, you normally don't need a linked list in Perl. Using an array, you can do all the same operations. The simple ones:
Add/remove list head: unshift/shift
Add/remove list tail: push/pop
As well as the slightly trickier ones. For a traditional linked list, you'll have a pointer to a node and for the array you'll just have an index into the list. So you can still remove the item you're pointing to:
or insert after the pointer:@list = (@list[0..($ptr-1)], @list[($ptr+1)..$#list]);
Notes:@list = (@list[0..$ptr], $item_to_insert, @list[($ptr+1)..$#list]);
So you don't usually need a linked list in Perl (except, of course, for homework assignments).
...roboticus
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: implementing linklist structure in perl
by jasonk (Parson) on May 09, 2007 at 15:46 UTC | |
by roboticus (Chancellor) on May 13, 2007 at 13:34 UTC |