On this page:
make-queue
enqueue!
dequeue!
queue-empty?
queue?
queue/ c
nonempty-queue/ c
Version: 4.1.4.3

4 Imperative Queues

 (require (planet cce/scheme:3:0/queue))

This module provides a mutable queue representation.

(make-queue)  queue/c

Produces an empty queue.

(enqueue! q v)  void?
  q : queue/c
  v : any/c

Adds an element to the back of a queue.

(dequeue! q)  any/c
  q : nonempty-queue/c

Removes an element from the front of a nonempty queue, and returns that element.

Examples:

  (define q (make-queue))
  > (enqueue! q 1)
  > (dequeue! q)

  1

  ; attempt to remove from empty queue
  > (dequeue! q)

  'program broke the contract (-> "nonempty-queue" any/c) on

  dequeue!; expected <nonempty-queue>, given: #<queue>

  > (enqueue! q 2)
  > (enqueue! q 3)
  > (dequeue! q)

  2

  > (dequeue! q)

  3

  ; attempt to remove from empty queue
  > (dequeue! q)

  'program broke the contract (-> "nonempty-queue" any/c) on

  dequeue!; expected <nonempty-queue>, given: #<queue>

(queue-empty? q)  boolean?
  q : queue/c

Recognizes whether a queue is empty or not.

Examples:

  (define q (make-queue))
  > (queue-empty? q)

  #t

  > (enqueue! q 1)
  > (queue-empty? q)

  #f

  > (dequeue! q)

  1

  > (queue-empty? q)

  #t

(queue? v)  boolean?
  v : any/c

This predicate recognizes queues.

Examples:

  > (queue? (make-queue))

  #t

  > (queue? 'not-a-queue)

  #f

queue/c : flat-contract?
nonempty-queue/c : flat-contract?

These contracts recognize queues; the latter requires the queue to contain at least one value.