新增链表
This commit is contained in:
parent
78a081e8db
commit
a266baff2a
|
@ -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
|
||||||
|
|
|
@ -24,5 +24,7 @@ module RedBlack =
|
||||||
elif y < x then balance (color, a, y, loop b)
|
elif y < x then balance (color, a, y, loop b)
|
||||||
else s
|
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节点"
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
<Compile Include="Algrithms/Sorts/InsertSort.fs" />
|
<Compile Include="Algrithms/Sorts/InsertSort.fs" />
|
||||||
<Compile Include="Algrithms/Sorts/BubbleSort.fs" />
|
<Compile Include="Algrithms/Sorts/BubbleSort.fs" />
|
||||||
<Compile Include="Algrithms/Sorts/Test.fs" />
|
<Compile Include="Algrithms/Sorts/Test.fs" />
|
||||||
|
<Compile Include="DataStructures/Lists/Link.fs"/>
|
||||||
<Compile Include="DataStructures/Trees/RedBlack.fs"/>
|
<Compile Include="DataStructures/Trees/RedBlack.fs"/>
|
||||||
<Compile Include="Monads/ReaderMonad.fs" />
|
<Compile Include="Monads/ReaderMonad.fs" />
|
||||||
<Compile Include="Monads/FreeMonad.fs" />
|
<Compile Include="Monads/FreeMonad.fs" />
|
||||||
|
|
Loading…
Reference in New Issue