Queue/Definition: Difference between revisions
→A linear linked list as a queue
Line 1,300:
(*------------------------------------------------------------------*)
vtypedef
(* A list that forms the queue, and a pointer to its last node. *)
@(list_vt (vt, n), ptr)
Line 1,308:
fn {}
() :
@(NIL, the_null_ptr)
fn {}
queue_vt_is_empty
{n : int}
{vt : vt@ype}
(q : !
[is_empty : bool | is_empty == (n == 0)]
bool is_empty =
Line 1,325:
fn {vt : vt@ype}
queue_vt_enqueue
{n : int}
(q :
x : vt) :
(* Returns the new queue. *)
[m : int | 1 <= m; m == n + 1]
let
val @(lst, tail_ptr) = q
Line 1,367:
(* The dequeue routine simply CANNOT BE CALLED with an empty queue.
It requires a queue of type
fn {vt : vt@ype}
queue_vt_dequeue
{n : int | 1 <= n}
(q :
(* Returns a tuple: the dequeued element and the new queue. *)
[m : int | 0 <= m; m == n - 1]
@(vt,
case+ q.0 of
| ~ (x :: lst) => @(x, @(lst, q.1))
(*
extern fun {vt : vt@ype}
fn {vt : vt@ype}
(q :
void =
let
Line 1,392:
| ~ (hd :: tl) =>
begin
loop tl
end
Line 1,403:
(* An example: a queue of nonlinear strings. *)
vtypedef
fn {} (* A parameterless template, for efficiency. *)
fn {} (* A parameterless template, for efficiency. *)
[is_empty : bool | is_empty == (n == 0)] bool is_empty =
fn
[m : int | 1 <= m; m == n + 1]
fn (* Impossible to... VVVVVV ...call with an empty queue. *)
[m : int | 0 <= m; m == n - 1] @(string,
fn
let
implement
(* A nonlinear string will be allowed to leak. (It might be
collected as garbage, however.) *)
begin end
in
end
macdef qnil =
overload iseqz with
overload << with
overload pop with
overload free with
implement
Line 1,488:
x = four
iseqz q = true</pre>
=={{header|AutoHotkey}}==
|