diff options
Diffstat (limited to 'list')
| -rw-r--r-- | list/list.gpr | 5 | ||||
| -rw-r--r-- | list/src/list.adb | 46 |
2 files changed, 51 insertions, 0 deletions
diff --git a/list/list.gpr b/list/list.gpr new file mode 100644 index 0000000..5095383 --- /dev/null +++ b/list/list.gpr | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | project List is | ||
| 2 | for Source_Dirs use ("src"); | ||
| 3 | for Object_Dir use "obj"; | ||
| 4 | for Main use ("list.adb"); | ||
| 5 | end List; | ||
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; | ||
