Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; converts a dlist into a standard common lisp list
- ;;; PARAMETERS:
- ;;; lst - dlist
- ;;; RETURNS:
- ;;; li - common lisp list
- (defmethod dlistify ((lst dlist))
- (let ((cur-n nil) (li nil))
- (setf cur-n (dlist-tail lst))
- (loop while cur-n do
- (push (node-data cur-n) li)
- (setf cur-n (node-prev cur-n))) li))
- ;;; appends the data to the end of the list
- ;;; PARAMETERS:
- ;;; lst - dlist
- ;;; data - the data that is being appended to the list
- ;;; RETURNS:
- ;;; lst - dlist
- (defmethod dpush (data (lst dlist))
- (dlist-append (make-node data) lst))
- ;;; prepends the data to the begging of the list
- ;;; PARAMETERS:
- ;;; lst - dlist
- ;;; data - the data that is being prepended to the list
- ;;; RETURNS:
- ;;; lst - dlist
- (defmethod dprepend (data (lst dlist))
- (dlist-prepend (make-node data) lst))
- ;;; removes the last item from the dlist and returns (pops) it
- ;;; NOTE:
- ;;; function will return nil if applied on an empty dlist !
- ;;; PARAMETERS:
- ;;; lst - dlist
- ;;; RETURNS:
- ;;; data - the data contained in the popped node
- (defmethod dpop ((lst dlist))
- (if (= (dlist-size lst) 0) (return-from dpop nil))
- (if (= (dlist-size lst) 1)
- (progn
- (let ((old-node (copy-structure (dlist-head lst))))
- (setf (dlist-head lst) nil)
- (setf (dlist-size lst) 0) (return-from dpop old-node))))
- (let ((old-node (copy-structure (dlist-tail lst))))
- (setf (dlist-tail lst) (node-prev (dlist-tail lst)))
- (setf (node-next (dlist-tail lst)) nil)
- (setf (dlist-size lst) (- (dlist-size lst) 1))
- (node-data old-node)))
- ;;; returns the size of the dlist
- ;;; PARAMETERS:
- ;;; lst - dlist
- ;;; RETURNS:
- ;;; size - integer size of the list
- (defmethod dsize ((lst dlist)) (dlist-size lst))
- ;;; returns the last item from the dlist without removing it
- ;;; PARAMETERS:
- ;;; lst - dlist
- ;;; RETURNS:
- ;;; data - the data contained in the last node
- (defmethod dpeek ((lst dlist)) (node-data (dlist-tail lst)))
- ;;; clears all items from the dlist
- ;;; PARAMETERS:
- ;;; lst - dlist
- ;;; RETURNS:
- ;;; lst - dlist (which is now cleared / empty)
- (defmethod dclear ((lst dlist))
- (setf (dlist-head lst) nil)
- (setf (dlist-tail lst) nil)
- (setf (dlist-size lst) 0) lst)
- ;;; compares dlist a to dlist b
- ;;; PARAMETERS:
- ;;; a - dlist
- ;;; b - dlist
- ;;; RETURNS:
- ;;; result - boolean
- (defmethod dequals ((a dlist) (b dlist))
- (if (not (equal (dlist-size a) (dlist-size b))) (return-from dequals nil))
- (let ((n-a (dlist-head a)) (n-b (dlist-head b)))
- (loop for i from 1 to (dlist-size a) do
- (if (not (equal (node-data n-a) (node-data n-b)))
- (return-from dequals nil))
- (setf n-a (node-next n-a))
- (setf n-b (node-next n-b)))) t)
- ;;; creates a dlist from a given list
- ;;; PARAMETERS:
- ;;; lst - standard common lisp list
- ;;; RETURNS:
- ;;; dlist - the dlist created from the given list
- (defmethod to-dlist ((lst list))
- (let ((dlst (make-dlist)))
- (loop for itm in lst do
- (dpush itm dlst)) dlst))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement