-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue-proc.rkt
60 lines (52 loc) · 1.48 KB
/
queue-proc.rkt
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#lang racket
(require compatibility/mlist)
;; A procedural queue implementation
(define (make-queue)
(let ((front-ptr '())
(rear-ptr '()))
(define (empty?) (null? front-ptr))
(define (front)
(if (empty?)
(error "FRONT called with an empty queue")
(mcar front-ptr)))
(define (insert! item)
(let ((new-pair (mcons item '())))
(cond ((empty?)
(set! front-ptr new-pair)
(set! rear-ptr new-pair)
(print))
(else
(set-mcdr! rear-ptr new-pair)
(set! rear-ptr new-pair)
(print)))))
(define (delete!)
(cond ((empty?)
(error "DELETE! called with an empty queue"))
(else
(set! front-ptr (mcdr front-ptr))
(print))))
(define (print)
(mlist->list front-ptr))
(define (dispatch m)
(cond
((eq? m 'empty?) empty)
((eq? m 'front) front)
((eq? m 'insert!) insert!)
((eq? m 'delete!) delete!)
((eq? m 'print) print)
(else (error "QUEUE -- unrecognized operation" 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!)))
(define (print-queue queue)
((queue 'print)))
(define q (make-queue))
(insert-queue! q 'a)
(insert-queue! q 'b)
(insert-queue! q 'c)