Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
73 lines (62 sloc) 1.36 KB
(* part 1 *)
(* file: dlist.ml *)
open Core.Std
type 'a element =
{ value : 'a;
mutable next : 'a element option;
mutable prev : 'a element option
}
type 'a t = 'a element option ref
(* part 2 *)
let create () = ref None
let is_empty t = !t = None
let value elt = elt.value
let first t = !t
let next elt = elt.next
let prev elt = elt.prev
(* part 3 *)
let insert_first t value =
let new_elt = { prev = None; next = !t; value } in
begin match !t with
| Some old_first -> old_first.prev <- Some new_elt
| None -> ()
end;
t := Some new_elt;
new_elt
(* part 4 *)
let insert_after elt value =
let new_elt = { value; prev = Some elt; next = elt.next } in
begin match elt.next with
| Some old_next -> old_next.prev <- Some new_elt
| None -> ()
end;
elt.next <- Some new_elt;
new_elt
(* part 5 *)
let remove t elt =
let { prev; next; _ } = elt in
begin match prev with
| Some prev -> prev.next <- next
| None -> t := next
end;
begin match next with
| Some next -> next.prev <- prev;
| None -> ()
end;
elt.prev <- None;
elt.next <- None
(* part 6 *)
let iter t ~f =
let rec loop = function
| None -> ()
| Some el -> f (value el); loop (next el)
in
loop !t
let find_el t ~f =
let rec loop = function
| None -> None
| Some elt ->
if f (value elt) then Some elt
else loop (next elt)
in
loop !t