Page 1 of 1

want to make stack

Posted: Fri Apr 20, 2012 1:56 am
by bhupi
;; IN THIS PROGRAM I WANT TO CHANGE REMOVE-FRONT FUNCTION TO RETURN ;;VALUES IN REVERSE ORDER LIKE
;;IF I ENTER a c b it returns a then c then b but i want that b must be returned first ;;then c then a
(defstruct q
(key #'identity)
(last nil)
(elements nil))



(defun make-empty-queue () (make-q))

(defun empty-queue? (q)
"Are there no elements in the queue?"
(= (length (q-elements q)) 0))

(defun queue-front (q)
"Return the element at the front of the queue."
(elt (q-elements q) 0))

(defun remove-front (q)
"Remove the element from the front of the queue and return it."
(if (listp (q-elements q))
(pop (q-elements q))
))



(defun enqueue-at-front (q items)
"Add a list of items to the front of the queue."
(setf (q-elements q) (nconc items (q-elements q))))

Re: want to make stack

Posted: Fri Apr 20, 2012 3:35 pm
by nuntius
Please provide attribution when you reuse code from others.
http://aima.cs.berkeley.edu/lisp/utilities/queue.lisp

CL already has PUSH and POP for stack manipulations. If a normal project, these are the default choice. In a homework assignment, both should be avoided; here, you want tools like CONS, CAR, CDR, and SETF. Plus there's the built-in REVERSE function.

Are you working on SICP exercise 2.18? There are a few elegant recursive solutions to the problem, as well as the usual iterative solutions.