小编Yan*_*vid的帖子

在Perl中实现一个节点列表

我编写了以下模块但不确定如何引用"last"和"head"节点.以及将下一个节点的地址存储在前一节点的"{nextNode}"中.

我试图在存储它时保存类的引用,但后来它抱怨:"不是List.pm中的HASH引用"; 我明白为什么但不确定语法是怎样的.

如果我取消引用$ head和$ last($$ last - > {nextNode} =\$ class),那么我认为它正在使用我的类的实际名称; 列出而不是我想要的上一个对象.

package List;

my $head = undef;
my $last = undef;

sub new {
    my $class = shift;

    # init the head of the list
    if ($head == undef) {
    $head = \$class;
    print "updated head to:$head", "\n";
    }

    $last = \$class;
    $last->{nextNode} = \$class; # update previous node to point on this new one    

    print "updated last to:$last", "\n";
    my $self = {};
    $self->{value} = shift;    
    $self->{nextNode} …
Run Code Online (Sandbox Code Playgroud)

perl implementation list

4
推荐指数
1
解决办法
107
查看次数

标签 统计

implementation ×1

list ×1

perl ×1