equal
deleted
inserted
replaced
|
1 ;;; <queue.ss> ---- Excessively simple queue implementation. |
|
2 ;;; Copyright (C) 2004 by Tony Garnock-Jones. |
|
3 |
|
4 ;;; This is free software; you can redistribute it and/or |
|
5 ;;; modify it under the terms of the GNU Lesser General Public |
|
6 ;;; License as published by the Free Software Foundation; either |
|
7 ;;; version 2.1 of the License, or (at your option) any later version. |
|
8 |
|
9 ;;; This software is distributed in the hope that it will be useful, |
|
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 ;;; Lesser General Public License for more details. |
|
13 |
|
14 ;;; You should have received a copy of the GNU Lesser General Public |
|
15 ;;; License along with this software; if not, write to the Free Software |
|
16 ;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 |
|
18 ;;; Author: Tony Garnock-Jones <tonyg@kcbbs.gen.nz> |
|
19 |
|
20 (module queue mzscheme |
|
21 (provide make-q enq! deq! q->list list->q q-empty?) |
|
22 |
|
23 (define (make-q) |
|
24 (cons '() '())) |
|
25 |
|
26 (define (enq! q e) |
|
27 (let ((cell (cons e '()))) |
|
28 (if (null? (car q)) |
|
29 (set-car! q cell) |
|
30 (set-cdr! (cdr q) cell)) |
|
31 (set-cdr! q cell))) |
|
32 |
|
33 (define (deq! q) |
|
34 (if (null? (car q)) |
|
35 #f |
|
36 (let ((v (caar q))) |
|
37 (set-car! q (cdar q)) |
|
38 (if (null? (car q)) |
|
39 (set-cdr! q '())) |
|
40 v))) |
|
41 |
|
42 (define (q->list q) |
|
43 (car q)) |
|
44 |
|
45 (define (list->q lst) |
|
46 (let ((q (make-q))) |
|
47 (for-each (lambda (x) (enq! q x)) lst) |
|
48 q)) |
|
49 |
|
50 (define (q-empty? q) |
|
51 (null? (car q))) |
|
52 ) |