From: Kim Nguyễn Date: Tue, 13 May 2014 12:36:20 +0000 (+0200) Subject: Add two functions to the Node_list interface (and to the naive implementation): X-Git-Url: http://git.nguyen.vg/gitweb/?p=tatoo.git;a=commitdiff_plain;h=cfbd6490c8b03b820375f79ff4d009ed2d0252c3;hp=31d45495fda9a110fd348f8b492761c28b434ec9 Add two functions to the Node_list interface (and to the naive implementation): * pop discards the first element * append appends the element of the second argument at the end of the first one (in place). --- diff --git a/src/naive_node_list.ml b/src/naive_node_list.ml index b6f0ae4..1b32c47 100644 --- a/src/naive_node_list.ml +++ b/src/naive_node_list.ml @@ -41,9 +41,22 @@ let iter f l = loop l.head + + let length l = l.length let is_empty l = l.head == nil +let pop l = + if is_empty l then failwith "pop" + else + let v = l.head.node in + let nhead = l.head.next in + l.head <- nhead; + if nhead == nil then l.last <- nil; + v + + + let add n l = let ncell = { node = n; next = nil } @@ -72,6 +85,15 @@ let push_front n l = l.length <- l.length + 1; end +let append l1 l2 = + let len2 = l2.length in + if len2 != 0 then begin + l1.length <- l1.length + len2; + l1.last.next <- l2.head; + l1.last <- l2.last; + end; + l1 + let head l = l.head let last l = l.last let next i = i.next diff --git a/src/node_list.ml b/src/node_list.ml index e9bb58a..1856bb8 100644 --- a/src/node_list.ml +++ b/src/node_list.ml @@ -23,6 +23,8 @@ module type S = val add : node -> t -> unit val push_front : node -> t -> unit val push_back : node -> t -> unit + val pop : t -> node + val append : t -> t -> t val iter : (node -> unit) -> t -> unit val length : t -> int val is_empty : t -> bool