A queue is a first-in first-out memory. Objects can be stored one by one and can be extracted (or removed) one by one. Objects are extracted (or removed) in the same time order as they are stored. How to implement a queue with constant access time, i.e. independent of the length of the queue? (Compare this problem with that of recipe FunctionalQueue).

The solution shown in this recipe is in essence the same as described in section 3.3.2 of SICP. However, in this recipe the word 'pointer' is avoided. Each queue will have its own set of six procedures, say queue-empty?, queue-enter!, queue-extract!, queue-peek, queue-remove! and queue->list.

1: (queue-empty? ) → #t if the queue is empty, else #f.

2: (queue-enter! object) → void, enters the object in the queue.

3: (queue-extract! [thunk]) → removes an object from the queue and returns that object.

4: (queue-peek [thunk]) → returns the next object of the queue without removing it.

5: (queue-remove! [thunk]) → void, removes an object from the queue.

6: (queue->list ) → copy of the queue (a newly allocated list with the same (eq?) contents as the queue)

These six procedures share access to a freshly allocated queue. The queue can be accessed by means of these six procedures only. Every distinct queue has its own distinct set of six procedures. Initially the queue is empty. When the queue is empty, the procedures queue-extract!, queue-peek or queue-remove! return whatever is returned by the thunk or signal an error if no thunk is supplied. The procedures are prepared by means of procedure make-queue:

(make-queue [string]) → multiple value of six procedures

The optional string is used in error messages in order to identify the queue. It has no other use. The default string is "no name".

The internal representation for the queue is a list containing the entered objects in the reversed order, i.e. the most recent one at the end and the oldest one at the start. Procedure queue-enter! appends an object at the end of the queue. This is done in situ (i.e. destructively) Procedures queue-extract!, queue-peek and queue-remove! apply to the first element of the queue. Procedures queue-extract! and queue-remove! replace the queue by its cdr, thus removing the oldest element. A simple, but inefficient implementation (without names, thunks or error-detection) is

500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

In this implementation, procedure queue-enter! is inefficient, because it calls procedure last-pair, which does a full traversal of the queue. In order to avoid this traversal, the last pair must be memorized:

500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

Example of use:

500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

Now it is a matter of routine in order to prepare the full implemention, name, thunk arguments and error detection included:

500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

A good alternative is to implement the queues as a class of objects with two private variables queue and last-pair and methods empty?, enter!, extract!, peek, remove!, copy->list and name: