code-ninja has asked for the wisdom of the Perl Monks concerning the following question:
I'm sure that I'm making a very novice mistake.
The insertion works perfectly. The output is as follows:#!/usr/bin/perl use strict; use warnings; use constant NEXT => 0; use constant VAL => 1; my ($list, $tail); my($pred, $temp); $list = $tail = undef; # insertion. A linked queue. foreach (1 .. 5) { my $node = [undef, $_ * $_]; if(not defined $tail) { $list = $tail = $node; } else { $tail->[NEXT] = $node; $tail = $node; } } $temp = $list; # print the values in the list. while (defined $temp) { print $temp->[VAL], "\n"; $temp = $temp->[NEXT]; } # deletion print "Enter element to be destroyed\n"; my $ele = <>; chomp $ele; $pred = $list; $temp = $list->[NEXT]; while (defined $temp) { if($temp->[VAL] == $ele) { $pred->[NEXT] = $temp->[NEXT]; print "$temp->[VAL] found!\n"; $temp->[NEXT] = undef; last; } else { $temp = $temp->[NEXT]; } } print "list after deleting $ele\n"; while (defined $list) { print $list->[VAL], "\n"; $list = $list->[NEXT]; }
now when I delete an element, I entered 4 and the output was the same minus 4. But when I delete 16, the output is:1 4 9 16 25
where am I going wrong?1 25
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Linked List
by Ratazong (Monsignor) on Sep 04, 2013 at 06:51 UTC | |
by code-ninja (Scribe) on Sep 04, 2013 at 06:57 UTC | |
Re: Linked List
by RichardK (Parson) on Sep 04, 2013 at 08:42 UTC | |
by choroba (Cardinal) on Sep 04, 2013 at 10:20 UTC | |
by code-ninja (Scribe) on Sep 04, 2013 at 09:02 UTC |
Back to
Seekers of Perl Wisdom