Skip to content

Commit

Permalink
Added singly linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
smaludzi committed May 17, 2021
1 parent bac6d31 commit 5b53231
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
87 changes: 87 additions & 0 deletions docs/list.md
@@ -0,0 +1,87 @@
---
title: Singly Linked List
---

# Singly Linked List


```never
enum List { Empty, Node { value : int; tail : List; } }
```

```never
func list_add(list : List, value : int) -> List
{
var l = List::Empty;
l = list;
List::Node(value, l)
}
```

```never
func list_print(list : List) -> int
{
var node = List::Empty;
node = list;
while (node != List::Empty)
{
if let (List::Node(value, tail) = node)
{
prints(value + "\n");
node = tail
}
else
{
node = List::Empty
}
}
}
```

```never
func list_print_rec(list : List) -> int
{
if let (List::Node(value, tail) = list)
{
prints(value + "\n");
list_print_rec(tail)
}
else
{
0
}
}
```

```never
func list_print_rec_2(list : List) -> int
{
match (list)
{
List::Empty -> 0;
List::Node(value, tail) -> { prints(value + "\n"); list_print_rec_2(tail) };
}
}
```

```never
func main() -> int
{
var list_rec = List::Node(10, List::Node(20, List::Node(30, List::Node(40, List::Empty))));
list_print_rec(list_rec);
list_print_rec_2(list_rec);
var list = List::Empty;
list = list_add(list, 40);
list = list_add(list, 30);
list = list_add(list, 20);
list = list_add(list, 10);
list_print(list);
0
}
```

1 change: 1 addition & 0 deletions mkdocs.yml
Expand Up @@ -13,6 +13,7 @@ nav:
- Never on the Web: never-web.md
- Sorting - Quick Sort: qsort.md
- Data Structures - Binary Tree: btree.md
Data Structures - Singly Linked List: list.md
- Dynamic Programming - Rod Cutting : rodcut.md
- Tribute to Alonzo C.: curradd.md
- Filter Map Reduce: fmr.md
Expand Down

0 comments on commit 5b53231

Please sign in to comment.