The Queue
structure provides an imperative queue data structure,
which are implemented as a pair of stacks (lists) representing the
front and rear of the queue. Single-threaded enqueuing and
dequeuing operations will have amortized constant time.
Synopsis
signature QUEUE
structure Queue :> QUEUE
Interface
type 'a queue
exception Dequeue
val mkQueue : unit -> 'a queue
val clear : 'a queue -> unit
val isEmpty : 'a queue -> bool
val enqueue : 'a queue * 'a -> unit
val dequeue : 'a queue -> 'a
val next : 'a queue -> 'a option
val delete : ('a queue * ('a -> bool)) -> unit
val head : 'a queue -> 'a
val peek : 'a queue -> 'a option
val length : 'a queue -> int
val contents : 'a queue -> 'a list
val app : ('a -> unit) -> 'a queue -> unit
val map : ('a -> 'b) -> 'a queue -> 'b queue
val foldl : ('a * 'b -> 'b) -> 'b -> 'a queue -> 'b
val foldr : ('a * 'b -> 'b) -> 'b -> 'a queue -> 'b
Description
type 'a queue
-
The type constructor for queues.
exception Dequeue
-
This exceptions is raised when the
dequeue
function is applied to an empty queue. val mkQueue : unit -> 'a queue
-
mkQueue ()
returns a new empty queue. val clear : 'a queue -> unit
-
clear q
removes any elements fromq
leaving it empty. val isEmpty : 'a queue -> bool
-
ifEmpty q
returns true if the queue is empty. val enqueue : 'a queue * 'a -> unit
-
enqueue (q, x)
addsx
to the end ofq
. val dequeue : 'a queue -> 'a
-
dequeue q
removes and returns the first element inq
. This function raises theDequeue
exception if it is called on an empty queue. val next : 'a queue -> 'a option
-
next q
returnsSOME x
and removesx
fromq
, wherex
was the first element inq
, orNONE
ifq
is empty. val delete : ('a queue * ('a -> bool)) -> unit
-
delete (q, pred)
removes those items fromq
for which the functionpred
returnstrue
. val head : 'a queue -> 'a
-
head q
returns the first element ofq
or raises the exceptionDequeue
ifq
is empty. The queue is unchanged. val peek : 'a queue -> 'a option
-
peek q
returnsSOME x
, wherex
is the first element ofq
, orNONE
ifq
is empty. The queue is unchanged. val length : 'a queue -> int
-
length q
returns the number of elements in the queue. val contents : 'a queue -> 'a list
-
contents q
returns the contents ofq
as a list. val app : ('a -> unit) -> 'a queue -> unit
-
app f q
applies the functionf
to the elements ofq
. This expression is equivalent toList.app f (contents q)
val map : ('a -> 'b) -> 'a queue -> 'b queue
-
map f q
returns a new queue that results from mapping the functionf
across the elements of the queue. val foldl : ('a * 'b -> 'b) -> 'b -> 'a queue -> 'b
-
foldl f init q
folds the functionf
over the elements ofq
from front to back. This expression is equivalent toList.foldl f init (contents q)
val foldr : ('a * 'b -> 'b) -> 'b -> 'a queue -> 'b
-
foldr f init q
folds the functionf
over the elements ofq
from back to front. This expression is equivalent toList.foldr f init (contents q)