23 Queue Operations

A queue is a first-in, first-out store of information. This implementation of queues uses difference-lists, the head of the difference-list represents the beginning of the queue and the tail represents the end of the queue. The members of the difference-list are the elements in the queue. The first argument in the queue-representation is the number of elements in the queue in unary representation.

Thus, a queue with n elements is represented as follows:

     q(s(...s(0)...), [X1,...,Xn,Y1,...,Ym], [Y1,...,Ym])

where n is the length of the queue and X1...Xn are the elements of the queue.

To load the package, enter the query

     | ?- use_module(library(queues)).
empty_queue(?Queue)
Is true if Queue has no elements.
is_queue(+Queue)
is true when Queue is a valid queue.
queue(?X, ?Queue)
Is true if Queue has one element and that is X.
queue_head(?Head, ?Queue1, ?Queue2)
Queue1 and Queue2 are the same queues except that Queue2 has Head inserted in the front. It can be used to enqueue the first element in Queue2. Example:
          | ?- queue_head(Head, Nq,
                          q(s(s(s(s(0)))),[1,2,3,4|R],R)).
          
          Head = 1,
          Nq = q(s(s(s(0))),[2,3,4|_193],_193),
          R = _193
     

queue_head_list(+HeadList, ?Queue1, ?Queue2)
Queue1 and Queue2 have the same elements except that Queue2 has HeadList inserted in the front.
queue_last(?Last, ?Queue1, ?Queue2)
Queue2 is like Queue1 but have Last as the last element in the queue.
queue_last_list(+LastList, ?Queue1, ?Queue2)
Queue1 and Queue2 are the same queues except that Queue2 has the list of elements LastList last in the queue. Example:
          | ?- queue_last_list([5,6], q(s(s(0)))), [1,2|R], R), NQ).
          
          NQ = q(s(s(s(s(0)))))),[1,2,5,6|_360],_360),
          R = [5,6|_360]
     

list_queue(+List, ?Queue)
Queue is the queue representation of the elements in List. Example:
          | ?- list_queue([1,2,3,4], Q).
          
          Q = q(s(s(s(s(0)))),[1,2,3,4|_138],_138)
     

queue_length(+Queue, ?Length)
Length is the number of elements in Queue. Example:
          | ?- queue_length(q(s(s(s(s(s(0))))),[a,b,c,d,e|R],R), L).
          
          L = 5,
          R = _155