【问题标题】:Issues assigning a string to a linked list in Ada在 Ada 中将字符串分配给链表的问题
【发布时间】:2013-03-15 06:36:39
【问题描述】:

我正在尝试引入一个字符串并将其逐个字符地分配给一个链接列表,声明如下:

type lstring is private;
type lstring_node;
type lstring_access is access lstring_node;
type lstring_node is
    record
        Char : character;
        Position : integer;
        Next : lstring_access;
    end record;

private
   type lstring is 
      record
          First : lstring_access := null;
      end record;

分配它的函数如下:

function toLstring (Input : string) return lstring is
    LStr : lstring;
    Current : lstring_access;
begin
    -- Set the first item of LStr as Current.
    -- LStr will from now on inherit the items of Current.
    LStr.First := new lstring_node;
    Current := LStr.First;

    -- Iterate through the string and add it to the lstring.
    for I in 1..Input'Length loop
        if I /= 1 then
            Current := new lstring_node;
        end if;
        Current.Char := Input(I);
        Ada.Text_IO.Put(Current.Char);
        Current.Position := I;
        Current := Current.Next;
    end loop;

    -- Return the new lstring.  
    return LStr;
end toLstring;

我通过调试知道 for 循环工作正常,并且元素被分配给 Current 就好了。但由于某种原因,这些项目没有被添加到 LStr。我需要在 for 循环之后声明一些东西来完成它吗?我的印象是,由于 Current 被分配给 LStr.First,LStr 将继承附加列表的其余部分。我错了吗?

谢谢

【问题讨论】:

  • 顺便说一句,这一定是我遇到过的最可怕的自定义字符串类型。

标签: pointers for-loop linked-list ada


【解决方案1】:

在循环结束时,将Current.Next(此时为空)分配给Current。这是一个价值副本。在下一次迭代中更改Current 的值不会更改前一个节点中Next 的值。 (请注意 Current.CharCurrent.Next 是隐式取消引用,实际上会执行 Current.all.Char / Next,但 Current := new lstring_node 不是取消引用,因为它会更改引用的值。)

相反,您应该将new lstring_node 分配给Next,然后推进您的Current 参考:

for I in Input'Range loop
    Current.Char := Input(I);
    Ada.Text_IO.Put(Current.Char);
    Current.Position := I - Input'First + 1;
    if I /= Input'Last then
        Current.Next := new lstring_node;
        Current := Current.Next;
    end if;
end loop;

请注意,我更改了循环范围(字符串不需要从 1 开始)并调整了位置计算,因此您最终会在列表中得到一个基于 1 的位置索引。

【讨论】:

  • 啊,谢谢!完美运行。没这么想过。
猜你喜欢
  • 1970-01-01
  • 2021-06-17
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 2014-01-04
  • 2021-01-10
相关资源
最近更新 更多