summaryrefslogtreecommitdiff
path: root/list/src/list.adb
blob: c8910d6dd6c5f6df6270fddd52fc1d3e0d49351f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
with Ada.Text_IO; use Ada.Text_IO;

procedure List is

  type MyList;

  type MyList_Access is access MyList;

  type MyList is record
    Value : Integer       := 0;
    Next  : MyList_Access := null;
  end record;

  function Length (XS : access constant MyList) return Integer is
    L    : Integer                := 0;
    Node : access constant MyList := XS;
  begin
    while Node /= null loop
      L    := L + 1;
      Node := Node.Next;
    end loop;
    return L;
  end Length;

  procedure Print_List (XS : access constant MyList) is
  begin
    if XS /= null then
      Put (Integer'Image (XS.Value) & " ");
      Print_List (XS.Next);
    end if;
  end Print_List;

  function Build_List return MyList_Access is
    XS : MyList_Access := new MyList'(1, new MyList'(2, new MyList'(3, null)));
  begin
    return XS;
  end Build_List;

  XS : MyList_Access := Build_List;

begin
  Put ("List: ");
  Print_List (XS);
  New_Line;
  Put_Line ("The list has length " & Integer'Image (Length (XS)));
end List;