Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.02 KB | None | 0 0
  1.  
  2. ;;; converts a dlist into a standard common lisp list
  3. ;;; PARAMETERS:
  4. ;;;     lst - dlist
  5. ;;; RETURNS:
  6. ;;;     li - common lisp list
  7. (defmethod dlistify ((lst dlist))
  8.   (let ((cur-n nil) (li nil))
  9.     (setf cur-n (dlist-tail lst))
  10.     (loop while cur-n do
  11.      (push (node-data cur-n) li)
  12.      (setf cur-n (node-prev cur-n))) li))
  13.  
  14. ;;; appends the data to the end of the list
  15. ;;; PARAMETERS:
  16. ;;;     lst - dlist
  17. ;;;     data - the data that is being appended to the list
  18. ;;; RETURNS:
  19. ;;;     lst - dlist
  20. (defmethod dpush (data (lst dlist))
  21.   (dlist-append (make-node data) lst))
  22.  
  23. ;;; prepends the data to the begging of the list
  24. ;;; PARAMETERS:
  25. ;;;     lst - dlist
  26. ;;;     data - the data that is being prepended to the list
  27. ;;; RETURNS:
  28. ;;;     lst - dlist
  29. (defmethod dprepend (data (lst dlist))
  30.   (dlist-prepend (make-node data) lst))
  31.  
  32. ;;; removes the last item from the dlist and returns (pops) it
  33. ;;; NOTE:
  34. ;;;     function will return nil if applied on an empty dlist !
  35. ;;; PARAMETERS:
  36. ;;;     lst - dlist
  37. ;;; RETURNS:
  38. ;;;     data - the data contained in the popped node
  39. (defmethod dpop ((lst dlist))
  40.   (if (= (dlist-size lst) 0) (return-from dpop nil))
  41.   (if (= (dlist-size lst) 1)
  42.       (progn
  43.     (let ((old-node (copy-structure (dlist-head lst))))
  44.       (setf (dlist-head lst) nil)
  45.       (setf (dlist-size lst) 0) (return-from dpop old-node))))
  46.   (let ((old-node (copy-structure (dlist-tail lst))))
  47.     (setf (dlist-tail lst) (node-prev (dlist-tail lst)))
  48.     (setf (node-next (dlist-tail lst)) nil)
  49.     (setf (dlist-size lst) (- (dlist-size lst) 1))
  50.     (node-data old-node)))
  51.  
  52. ;;; returns the size of the dlist
  53. ;;; PARAMETERS:
  54. ;;;      lst - dlist
  55. ;;; RETURNS:
  56. ;;;      size - integer size of the list
  57. (defmethod dsize ((lst dlist)) (dlist-size lst))
  58.  
  59. ;;; returns the last item from the dlist without removing it
  60. ;;; PARAMETERS:
  61. ;;;     lst - dlist
  62. ;;; RETURNS:
  63. ;;;     data - the data contained in the last node
  64. (defmethod dpeek ((lst dlist)) (node-data (dlist-tail lst)))
  65.  
  66. ;;; clears all items from the dlist
  67. ;;; PARAMETERS:
  68. ;;;     lst - dlist
  69. ;;; RETURNS:
  70. ;;;     lst - dlist (which is now cleared / empty)
  71. (defmethod dclear ((lst dlist))
  72.   (setf (dlist-head lst) nil)
  73.   (setf (dlist-tail lst) nil)
  74.   (setf (dlist-size lst) 0) lst)
  75.  
  76. ;;; compares dlist a to dlist b
  77. ;;; PARAMETERS:
  78. ;;;     a - dlist
  79. ;;;     b - dlist
  80. ;;; RETURNS:
  81. ;;;     result - boolean
  82. (defmethod dequals ((a dlist) (b dlist))
  83.   (if (not (equal (dlist-size a) (dlist-size b))) (return-from dequals nil))
  84.   (let ((n-a (dlist-head a)) (n-b (dlist-head b)))
  85.     (loop for i from 1 to (dlist-size a) do
  86.      (if (not (equal (node-data n-a) (node-data n-b)))
  87.          (return-from dequals nil))
  88.      (setf n-a (node-next n-a))
  89.      (setf n-b (node-next n-b)))) t)
  90.  
  91.  
  92. ;;; creates a dlist from a given list
  93. ;;; PARAMETERS:
  94. ;;;     lst - standard common lisp list
  95. ;;; RETURNS:
  96. ;;;     dlist - the dlist created from the given list
  97. (defmethod to-dlist ((lst list))
  98.   (let ((dlst (make-dlist)))
  99.     (loop for itm in lst do
  100. (dpush itm dlst)) dlst))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement