(* MyQueue: simple queues *) type 'a cell = { content : 'a; mutable next : 'a cell; } type 'a queue = { mutable size : int; mutable cell : 'a cell option; } let create () = { size = 0; cell = None; } let is_empty q = q.size = 0 let length q = q.size let push q x = begin q.size <- q.size + 1; match q.cell with | None -> q.cell <- Some (let rec c = {content = x; next = c} in c) | Some cell -> begin cell.next <- { content = x; next = cell.next; }; q.cell <- Some cell.next; end end let take q = begin match q.cell with | None -> assert false | Some cell -> let res = cell.next.content in begin q.size <- q.size - 1; if q.size = 0 then q.cell <- None else cell.next <- cell.next.next; res end end