首页 > 解决方案 > 如何在 Ada 中制作二维链表

问题描述

对于我正在处理的项目,我想使用 Ada.Collections.Doubly_Linked_Lists 创建一个包含字符串的 2d(列表列表)。我无法弄清楚如何在没有任何 3rd 方库的情况下完成这项工作。任何帮助表示赞赏!

标签: listdata-structuresnestedada

解决方案


我同意@Rich 和@trashgod 的评论,但仍然是一个小例子:

主文件

with Ada.Text_IO;    use Ada.Text_IO;
with Ada.Containers; use Ada.Containers;

with Ada.Containers.Doubly_Linked_Lists;
with Ada.Containers.Indefinite_Doubly_Linked_Lists;

procedure Main is

   --  Inner list (Strings are indefinite types).
   package L_Strings is
     new Indefinite_Doubly_Linked_Lists (String);
   use L_Strings;   

   --  Outer list.
   package L_L_Strings is
     new Doubly_Linked_Lists (L_Strings.List);
   use L_L_Strings;


   --  Cursors for pointing to elements in either lists.
   LC  :   L_Strings.Cursor;
   LLC : L_L_Strings.Cursor;

   --  Create an empty outer list.
   LLS : L_L_Strings.List := L_L_Strings.Empty_List;

begin

   --  Add two empty inner lists to the outer list.
   LLS.Append (L_Strings.Empty_List);
   LLS.Append (L_Strings.Empty_List);

   --  Add some strings to the first inner list (of strings).
   LLC := First (LLS);
   LLS (LLC).Append("This");
   LLS (LLC).Append("is");
   LLS (LLC).Append("a");

   --  Add some strings to the second inner list (of strings).
   LLC := Next (LLC);
   LLS (LLC).Append("nested");
   LLS (LLC).Append("linked");   
   LLS (LLC).Append("list");

   -- Loop over the elements of the outer and inner list.
   for Outer_Elem of LLS loop      
      for Inner_Elem of Outer_Elem loop         
         Put (Inner_Elem & " ");
      end loop;
      New_Line;
   end loop;

end Main;

输出

This is a 
nested linked list

推荐阅读