X-Git-Url: https://git.distorted.org.uk/~mdw/lisp/blobdiff_plain/2626af6648d844d428b3da1c12d1401e42405593..3dba250d40912df843708c98114a084c731f4132:/queue.lisp diff --git a/queue.lisp b/queue.lisp index 03de433..44ec534 100644 --- a/queue.lisp +++ b/queue.lisp @@ -22,10 +22,10 @@ ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. (defpackage #:queue - (:use #:common-lisp) - (:export #:make-queue #:queue-emptyp #:enqueue #:dequeue)) + (:use #:common-lisp)) (in-package #:queue) +(export 'make-queue) (defun make-queue () "Make a new queue object." ;; A queue is just a cons cell. The cdr is the head of the list of items @@ -35,16 +35,27 @@ (let ((q (cons nil nil))) (setf (car q) q))) +(export 'queue-emptyp) (defun queue-emptyp (q) "Answer whether the queue Q is empty." (null (cdr q))) +(export 'enqueue) (defun enqueue (x q) "Enqueue the object X into the queue Q." (let ((c (cons x nil))) (setf (cdr (car q)) c (car q) c))) +(export 'pushqueue) +(defun pushqueue (x q) + "Push the object X onto the front of the queue Q." + (let* ((first (cdr q)) + (new (cons x first))) + (setf (cdr q) new) + (unless first (setf (car q) new)))) + +(export 'dequeue) (defun dequeue (q) "Remove and return the object at the head of the queue Q." (if (queue-emptyp q) @@ -74,10 +85,12 @@ (let ((q (make-queue)) (want nil)) (dotimes (i 10000) - (case (random 2) + (case (random 3) (0 (setf want (nconc want (list i))) (enqueue i q)) - (1 (if (null want) + (1 (push i want) + (pushqueue i q)) + (2 (if (null want) (assert (queue-emptyp q)) (progn (let ((j (dequeue q))