-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.22.scm
34 lines (33 loc) · 1005 Bytes
/
3.22.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(define (make-queue)
(let ((front-ptr '())
(rear-ptr '()))
(define (set-front-ptr! item) (set! front-ptr item))
(define (set-rear-ptr! item) (set! rear-ptr item))
(define (empty?) (null? front-ptr))
(define (front)
(if (empty?)
(error "FRONT called with an empty queue" front-ptr)
(car front-ptr)))
(define (insert! item)
(let ((new-pair (cons item '())))
(if (empty?)
(set-front-ptr! new-pair)
(set-cdr! rear-ptr new-pair))
(set-rear-ptr! new-pair)
front-ptr))
(define (delete!)
(cond ((empty?)
(error "DELETE! called with an empty queue" front-ptr))
(else (set-front-ptr! (cdr front-ptr))
front-ptr)))
(define (dispatch m)
(cond ((eq? m 'empty?) empty?)
((eq? m 'insert!) insert!)
((eq? m 'front) front)
((eq? m 'delete!) delete!)
(else (error "Undefined operation: MAKE-QUEUE" m))))
dispatch))
(define (empty-queue? queue) ((queue 'empty)))
(define (front-queue queue) ((queue 'front)))
(define (insert-queue! queue item) ((queue 'insert!) item))
(define (delete-queue! queue) ((queue 'delete!)))