queue.scm 702 B

123456789101112131415161718192021222324252627
  1. (define (empty-queue) (list->vector (list 'queue '() #f)))
  2. (define (queue:top q) (vector-ref q 1))
  3. (define (queue:bot q) (vector-ref q 2))
  4. (define (queue:top! q v) (vector-set! q 1 v))
  5. (define (queue:bot! q v) (vector-set! q 2 v))
  6. (define (queue-push! q v)
  7. (if (queue:bot q)
  8. (begin (set-cdr! (queue:bot q) (list v))
  9. (queue:bot! q (cdr (queue:bot q))))
  10. (begin (queue:top! q (list v))
  11. (queue:bot! q (queue:top q)))))
  12. (define (queue-pop! q)
  13. (let ((top (queue:top q)))
  14. (if (null? top)
  15. (error 'queue-pop! "null")
  16. (begin
  17. (queue:top! q (cdr top))
  18. (when (null? (cdr top))
  19. (queue:bot! q #f))
  20. (car top)))))
  21. (define (queue->list q)
  22. (copy-list (queue:top q)))