File: queue

package info (click to toggle)
scheme9 2025.08.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: lisp: 16,752; ansic: 11,869; sh: 806; makefile: 237; sed: 6
file content (33 lines) | stat: -rw-r--r-- 1,158 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
S9 LIB  (make-queue)           ==>  queue
        (queue! queue object)  ==>  unspecific
        (unqueue! queue)       ==>  object
        (queue-empty? queue)   ==>  queue
        (unqueue* queue)       ==>  list

MAKE-QUEUE returns an empty queue. A queue is a data structure
that delivers unqueued elements in the same order in which they
were queued (a first-in first-out structure). Both enqueue and
dequeue operations are O(1).

QUEUE! (destructively) inserts an OBJECT at the input end
of QUEUE.

QUEUE-EMPTY? returns #T if QUEUE is empty.

UNQUEUE! (destructively) removes an element from the output end
of QUEUE and returns it.

UNQUEUE* removes an element from a queue and returns both the
element and the queue in a list of the form

      (element queue)

NOTE: although a queue may look like a list, it is actually
a (directed acylic) graph. Altering a queue with list operations
is therefore not recommended! Copying a queue with procedures
like TREE-COPY will turn it into a non-queue. Caveat utilitor!

(let ((q (make-queue)))
  (for-each (lambda (x) (queue! q x))
            '(a b c d e))
  (unqueue* q))            ==>  (a ((e) b c d e))