A
alexxx.magni
please forgive me if this is a dumb question, but I'm unable to code a
linked list in perl - to be precise I'm able to create it but I'm
unable to delete nodes...
the linked list contains 2 coordinates 'x','y' for each node:
my $pixies=undef;
for(1..10)
{ $pixies= [ $pixies, {'x'=>int(3*rand()), 'y'=>int(10*rand())} ] }
my $head=$pixies;
for $i(1..10)
{
print("$i\t",$pixies->[1]{'x'},"\t",$pixies->[1]{'y'},"\n");
$pixies=$pixies->[0];
}
# trying to delete the 8th node:
for $i(1..7)
{ $pixies=$pixies->[0] }
# now!
$pixies->[0]=$pixies->[0][0];
print"\n\n\tdeleted 8...\n\n\n";
# check:
$pixies=$head;
for $i(1..10)
{
print("$i\t",$pixies->[1]{'x'},"\t",$pixies->[1]{'y'},"\n");
$pixies=$pixies->[0];
}
and I got the same nodes again!!! What am I doing wrong?
Thanks a lot for any help on this...
P.S.: I read often that linked lists implemented this way are not the
way to go in perl, that you can do the same things with arrays (arrays
of hashes, in my case).
Do you agree? And in that case, how would you delete a node in the
middle of the array without too much copying around of large
structures?
thanks again
alessandro
linked list in perl - to be precise I'm able to create it but I'm
unable to delete nodes...
the linked list contains 2 coordinates 'x','y' for each node:
my $pixies=undef;
for(1..10)
{ $pixies= [ $pixies, {'x'=>int(3*rand()), 'y'=>int(10*rand())} ] }
my $head=$pixies;
for $i(1..10)
{
print("$i\t",$pixies->[1]{'x'},"\t",$pixies->[1]{'y'},"\n");
$pixies=$pixies->[0];
}
# trying to delete the 8th node:
for $i(1..7)
{ $pixies=$pixies->[0] }
# now!
$pixies->[0]=$pixies->[0][0];
print"\n\n\tdeleted 8...\n\n\n";
# check:
$pixies=$head;
for $i(1..10)
{
print("$i\t",$pixies->[1]{'x'},"\t",$pixies->[1]{'y'},"\n");
$pixies=$pixies->[0];
}
and I got the same nodes again!!! What am I doing wrong?
Thanks a lot for any help on this...
P.S.: I read often that linked lists implemented this way are not the
way to go in perl, that you can do the same things with arrays (arrays
of hashes, in my case).
Do you agree? And in that case, how would you delete a node in the
middle of the array without too much copying around of large
structures?
thanks again
alessandro