diff options
| author | 3gg <3gg@shellblade.net> | 2025-08-09 16:03:28 +0200 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-08-09 16:03:28 +0200 |
| commit | 727e3c59346da4f91284b34b4c18f2e0ba155e53 (patch) | |
| tree | 807dccd5cba3c6bae2f8d0c9910157e306c6da5b /list/src | |
Diffstat (limited to 'list/src')
| -rw-r--r-- | list/src/list.adb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/list/src/list.adb b/list/src/list.adb new file mode 100644 index 0000000..c8910d6 --- /dev/null +++ b/list/src/list.adb | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | with Ada.Text_IO; use Ada.Text_IO; | ||
| 2 | |||
| 3 | procedure List is | ||
| 4 | |||
| 5 | type MyList; | ||
| 6 | |||
| 7 | type MyList_Access is access MyList; | ||
| 8 | |||
| 9 | type MyList is record | ||
| 10 | Value : Integer := 0; | ||
| 11 | Next : MyList_Access := null; | ||
| 12 | end record; | ||
| 13 | |||
| 14 | function Length (XS : access constant MyList) return Integer is | ||
| 15 | L : Integer := 0; | ||
| 16 | Node : access constant MyList := XS; | ||
| 17 | begin | ||
| 18 | while Node /= null loop | ||
| 19 | L := L + 1; | ||
| 20 | Node := Node.Next; | ||
| 21 | end loop; | ||
| 22 | return L; | ||
| 23 | end Length; | ||
| 24 | |||
| 25 | procedure Print_List (XS : access constant MyList) is | ||
| 26 | begin | ||
| 27 | if XS /= null then | ||
| 28 | Put (Integer'Image (XS.Value) & " "); | ||
| 29 | Print_List (XS.Next); | ||
| 30 | end if; | ||
| 31 | end Print_List; | ||
| 32 | |||
| 33 | function Build_List return MyList_Access is | ||
| 34 | XS : MyList_Access := new MyList'(1, new MyList'(2, new MyList'(3, null))); | ||
| 35 | begin | ||
| 36 | return XS; | ||
| 37 | end Build_List; | ||
| 38 | |||
| 39 | XS : MyList_Access := Build_List; | ||
| 40 | |||
| 41 | begin | ||
| 42 | Put ("List: "); | ||
| 43 | Print_List (XS); | ||
| 44 | New_Line; | ||
| 45 | Put_Line ("The list has length " & Integer'Image (Length (XS))); | ||
| 46 | end List; | ||
