Next: , Previous: Dictionaries, Up: Compound Data Types


22.8 Queues

To use procedures for creating and manipulating queues, evaluate the form:

     (use-modules (ice-9 q))

A queue is a cons pair:

     ( the-q . last-pair )

the-q is a list of things in the queue. New elements go at the end of that list. last-pair is #f if the queue is empty, and otherwise is the last pair of the-q.

Queues print nicely, but alas, they do not read well because the eq?-ness of last-pair and (last-pair the-q) is lost by read.

All the procedures that aren't explicitly defined to return something else (a queue element; a boolean value) return the queue object itself.

— Scheme Procedure: sync-q! q

Recompute and reset the last-pair component of queue q.

— Scheme Procedure: make-q

Return a new queue.

— Scheme Procedure: q? obj

Return #t iff obj is a queue. An object is a queue if it is equal? to (() . #f) or if it is a pair whose car is a list and whose cdr is a eq? to the car's last pair.

— Scheme Procedure: q-empty? q

Return #t iff queue q contains no elements.

— Scheme Procedure: q-empty-check q

Check queue q and throw an exception if it contains no elements. The key for the throw is q-empty and the value q.

— Scheme Procedure: q-front q

Return the first element of queue q.

— Scheme Procedure: q-rear q

Return the last element of queue q.

— Scheme Procedure: q-remove! q obj

Remove all occurences of obj from queue q.

— Scheme Procedure: q-push! q obj

Add obj to the front of queue q.

— Scheme Procedure: enq! q obj

Add obj to the rear of queue q.

— Scheme Procedure: q-pop! q
— Scheme Procedure: deq! q

Take the front of queue q and return it.

— Scheme Procedure: q-length q

Return the number of enqueued elements in queue q.