5 (define (front-ptr deque) (car deque))
6 (define (rear-ptr deque) (cdr deque))
7 (define (set-front-ptr! deque item) (set-car! deque item))
8 (define (set-rear-ptr! deque item) (set-cdr! deque item))
10 (define (empty-deque? deque) (null? (front-ptr deque)))
12 (define (make-deque) (cons '() '()))
14 (define (front-deque deque)
15 (if (empty-deque? deque)
16 (error "FRONT called with an empty deque" deque)
17 (car (front-ptr deque))))
19 (define (rear-deque deque)
20 (if (empty-deque? deque)
21 (error "REAR called with an empty deque" deque)
22 (car (rear-ptr deque))))
24 (define (make-cell item)
25 (cons item (cons '() '())))
30 (define (next-cell cell)
33 (define (prev-cell cell)
36 (define (set-next! cell pair)
37 (set-car! (cdr cell) pair))
39 (define (set-prev! cell pair)
40 (set-cdr! (cdr cell) pair))
42 (define (front-insert-deque! deque item)
43 (let ((new-pair (make-cell item)))
46 (set-front-ptr! deque new-pair)
47 (set-rear-ptr! deque new-pair)
50 (set-next! new-pair (front-ptr deque))
51 ;(set-cdr! (cdr (front-ptr deque)) new-pair)
52 (set-prev! (front-ptr deque) new-pair)
53 (set-front-ptr! deque new-pair)
56 (define (rear-insert-deque! deque item)
57 (let ((new-pair (make-cell item)))
60 (set-front-ptr! deque new-pair)
61 (set-rear-ptr! deque new-pair)
64 (set-prev! new-pair (rear-ptr deque))
65 ;(set-car! (cdr (rear-ptr deque)) new-pair)
66 (set-next! (rear-ptr deque) new-pair)
67 (set-rear-ptr! deque new-pair)
70 (define (front-delete-deque! deque)
73 (error "DELETE! called with an empty deque"))
75 (set-front-ptr! deque (car (cdr (front-ptr deque))))
78 (define (rear-delete-deque! deque)
81 (error "DELETE! called with an empty deque"))
83 (set-rear-ptr! deque (prev-cell (rear-ptr deque)))
84 (set-next! (rear-ptr deque) '())
88 (define (print-deque queue)
89 (define (copy-queue q1)
90 (let ((q2 (make-deque)))
91 (set-front-ptr! q2 (front-ptr q1))
92 (set-rear-ptr! q2 (rear-ptr q1))
94 (let ((q (copy-queue queue)))
95 (if (not (empty-deque? q))
97 (display (front-deque q))
99 (print-deque (front-delete-deque! q))))))
102 > (define q (make-deque))
103 > (front-insert-deque! q 'a)
104 (mcons (mcons 'a (mcons '() '())) (mcons 'a (mcons '() '())))
105 > (front-insert-deque! q 'b)
106 (mcons #0=(mcons 'b (mcons #1=(mcons 'a (mcons '() #0#)) '())) #1#)
107 > (front-insert-deque! q 'c)
109 #0=(mcons 'c (mcons #1=(mcons 'b (mcons #2=(mcons 'a (mcons '() #1#)) #0#)) '()))
115 #0=(mcons 'c (mcons #1=(mcons 'b (mcons #2=(mcons 'a (mcons '() #1#)) #0#)) '()))
117 > (rear-delete-deque! q)
118 (mcons #0=(mcons 'c (mcons #1=(mcons 'b (mcons '() #0#)) '())) #1#)
121 > (rear-insert-deque! q 'd)
123 #0=(mcons 'c (mcons #1=(mcons 'b (mcons #2=(mcons 'd (mcons '() #1#)) #0#)) '()))
127 > (front-insert-deque! q 'e)
134 (mcons #2=(mcons 'b (mcons #3=(mcons 'd (mcons '() #2#)) #1#)) #0#))
139 > (front-delete-deque! q)
144 #1=(mcons 'b (mcons #2=(mcons 'd (mcons '() #1#)) #0#))
145 (mcons 'e (mcons #0# '()))))