diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /tigrou/linked-list | |
Diffstat (limited to 'tigrou/linked-list')
| -rw-r--r-- | tigrou/linked-list/linked-list.tig | 24 | ||||
| -rw-r--r-- | tigrou/linked-list/linked-list.tih | 15 |
2 files changed, 39 insertions, 0 deletions
diff --git a/tigrou/linked-list/linked-list.tig b/tigrou/linked-list/linked-list.tig new file mode 100644 index 0000000..04c8a29 --- /dev/null +++ b/tigrou/linked-list/linked-list.tig @@ -0,0 +1,24 @@ +let + import "linked-list.tih" + var list := node { value = 0, next = nil } + + function test_lists(expected: string) = + ( + print(concat("Expected : { ", expected)); + print(" }\ngot : { "); + display(list); + print(" }\n\n") + ) +in + test_lists("0"); + + print("Adding '1' ...\n"); + append(list, 1); + + test_lists("0 -> 1"); + + print("Adding '2' ...\n"); + append(list, 2); + + test_lists("0 -> 1 -> 2") +end diff --git a/tigrou/linked-list/linked-list.tih b/tigrou/linked-list/linked-list.tih new file mode 100644 index 0000000..b890f3a --- /dev/null +++ b/tigrou/linked-list/linked-list.tih @@ -0,0 +1,15 @@ +type node = {value : int, next : node} +function append(list: node, element: int) = + if list.next = nil then + list.next := node{value = element, next = nil} + else + append(list.next, element) + +function display(list: node) = + if list <> nil then + ( + print_int(list.value); + (if list.next <> nil then + print(" -> ")); + display(list.next) + ) |
