Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.