Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Created November 3, 2019 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/ac3f76a59ca7116a6f797fd10912a5b6 to your computer and use it in GitHub Desktop.
Save chelseatroy/ac3f76a59ca7116a6f797fd10912a5b6 to your computer and use it in GitHub Desktop.
Queue Concurrency
; 3.22
(define (make-queue)
(let ((front-ptr '())
(rear-ptr '()))
(define (empty?)
(null? front-ptr))
(define (insert! item)
(let ((new-pair (mcons item '())))
(cond ((empty?)
(set! front-ptr new-pair)
(set! rear-ptr new-pair)
)
(else
(set-mcdr! rear-ptr new-pair)
(set! rear-ptr new-pair)))))
(define (front)
(cond ((empty?) (error "Empty queue"))
(else (mcar front-ptr))))
(define (rear)
(cond ((empty?) (error "Empty queue"))
(else (mcar rear-ptr))))
(define (delete!)
(cond ((empty?) (error "Empty queue"))
(else (set! front-ptr (mcar front-ptr)))))
(define (dispatch msg)
(cond ((eq? msg 'insert!) insert!)
((eq? msg 'delete!) (delete!))
((eq? msg 'empty?) (empty?))
((eq? msg 'front) (front))
((eq? msg 'rear) (rear))
(else (error "Bad message"))))
dispatch
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment