新增链表

This commit is contained in:
Sanchime 2022-08-14 22:15:22 +08:00
parent 78a081e8db
commit a266baff2a
3 changed files with 42 additions and 1 deletions

View File

@ -0,0 +1,38 @@
namespace Sanchime.DataStructures.Lists
module Link =
exception EmptyList
type LinkList<'T> =
| Cons of 'T * 'T LinkList
| Nil
let first = function
| Nil -> raise EmptyList
| Cons(value, _) -> value
let last = function
| Nil -> Nil
| Cons(_, tail) -> tail
let length list =
let rec loop list count =
match list with
| Nil -> count
| Cons(_, tail) -> loop tail (count + 1)
loop list 0
let rec map mapper = function
| Nil -> Nil
| Cons(head, tail) -> Cons(mapper head, map mapper tail)
let rec append left right =
match left with
| Nil -> right
| Cons(head, tail) -> Cons(head, append tail right)
let empty = function
| Nil -> true
| Cons(_, _) -> false

View File

@ -24,5 +24,7 @@ module RedBlack =
elif y < x then balance (color, a, y, loop b)
else s
let (Node (_, a, b, c)) = loop s in Node (Black, a, b, c)
match loop s with
| Node (_, y, a, b) -> Node (Black, y, a, b)
| Leaf -> failwith "Leaf"

View File

@ -10,6 +10,7 @@
<Compile Include="Algrithms/Sorts/InsertSort.fs" />
<Compile Include="Algrithms/Sorts/BubbleSort.fs" />
<Compile Include="Algrithms/Sorts/Test.fs" />
<Compile Include="DataStructures/Lists/Link.fs"/>
<Compile Include="DataStructures/Trees/RedBlack.fs"/>
<Compile Include="Monads/ReaderMonad.fs" />
<Compile Include="Monads/FreeMonad.fs" />