;;; -*-LISP-*- ;;; ******* (C) COPYRIGHT 2003 DONALD FISK. ALL RIGHTS RESERVED ******** ;;; * EMBLEM RELEASE 012 * ;;; * FOR PERSONAL USE, TEACHING AND ACADEMIC RESEARCH ONLY. THIS * ;;; * PROGRAM IS SUBJECT TO THE EMBLEM NON-COMMERCIAL SHARED SOURCE * ;;; * LICENSE. * ;;; * THIS PROGRAM IS WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED * ;;; * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * ;;; ******************************************************************** (defclass Queue (Any) (elems type List accessor getElems initForm '()) (last type List accessor getLast initForm '() doc "the last cons cell")) (defmethod addElem ((x Any) (q Queue)) "Add arg1 to Queue arg2." (returns Queue) (cond ((null (getElems q)) ;q empty? (push x (getElems q)) ;OK to x to front of q. (setf (getLast q) (getElems q))) ;last of q = all the elems. (TRUE (setf (cdr (getLast q)) (list x)) ;Add x to back. (pop (getLast q)))) ;Prev last now second last, so pop it. q) ;Return q. (defmethod removeElem ((q Queue)) "Removes the elem at the front of Queue arg1." (returns Any) (cond ((null (getElems q)) '()) ;q empty => return '()? (TRUE (when (null (cdr (getElems q))) ;Only one elem? (pop (getLast q))) ;Remove it from last. (pop (getElems q))))) ;Remove and return first elem.