したがって、スタックオーバーフローには何百もの例があることを知っています。実際、そこからのすべての情報を使用しました-これが私が持っているものです
use strict;
use warnings;
use Data::Dumper;
my $head= undef;
my $tail=\$head;
open FILE, "<datastored.txt" or die $!;
while (<FILE>){
my $node = {
"data" => $_ ,
"next" => undef
};
$$tail=$node;
$tail = \$node->{"next"};
};
print Dumper $head; #before reversing
$head = reverse_list($head);
print Dumper $head; #after reversing
sub reverse_list{
my ($list) =@_[0];
my $previous = undef;
while ($list->{next}){
$forward = $list->{next};
$list->{next}= $previous;
$previous = $list;
$list=$forward;
};
return $previous;
};
これは私が得る出力です
#this is the output before reversing (normal linked list)
$VAR1 = {
'next' => {
'next' => {
'next' => {
'next' => undef,
'data' => 'line 4
'
},
'data' => 'line 3
'
},
'data' => 'line 2
'
},
'data' => 'line 1
'
};
#this is the linked list after reversing (WITHOUT THE LAST DATA VARIABLE - "line 4")
$VAR1 = {
'next' => {
'next' => {
'next' => undef,
'data' => 'line 1
'
},
'data' => 'line 2
'
},
'data' => 'line 3
'
};
注 - ファイルの内容datastored.txt
は単純です
line 1
line 2
line 3
line 4
だから私の質問は、データの「4行目」がどこにあるのか、実際に値を失うことなくリンクされたリストを逆にするために何を変更すればよいのかということです。