Go to the previous, next section.

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)).

@
Is true if Queue has no elements.

@
Is true if Queue has one element and that is X.

@
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 ? 

yes

@
Queue1 and Queue2 have the same elements except that Queue2 has HeadList inserted in the front.

@
Queue2 is like Queue1 but have Last as the last element in the queue.

@
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] ? 

yes
@
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) ? 

yes
| ?- 

@
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 ? 

yes

Go to the previous, next section.