ocaml-standard-library/lib/queue.ml

17 lines
438 B
OCaml
Raw Permalink Normal View History

open General
2021-12-16 10:01:08 +00:00
open List
let enqueue (a : 'a) (qu : 'a queue) : 'a queue =
match qu.front with
| [] -> { front = [a]; back = [] }
| _ -> { qu with back = a :: qu.back }
2021-12-16 10:01:08 +00:00
let dequeue (qu : 'a queue) : ('a option * 'a queue) =
match qu.front with
| [a] -> (Some a, { front = List.reverse qu.back; back = [] })
| h :: t -> (Some h, { qu with front = t })
| [] -> (None, qu)
let empty = { front = []; back = [] }