-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path3.23.rkt
94 lines (82 loc) · 3.27 KB
/
3.23.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
;; make deque by double list
(define (make-deque)
(let ((deque (cons '() '()))
(front-ptr car)
(rear-ptr cdr)
(set-front-ptr! set-car!)
(set-rear-ptr! set-cdr!))
(define (empty-deque? deque)
(null? (front-ptr deque)))
(define (front-deque)
(if (empty-deque? deque)
(error "FRONT called witn an empty deque" deque))
(car (front-ptr deque)))
(define (rear-deque)
(if (empty-deque? deque)
(error "REAR called witn an empty deque" deque)
(car (rear-ptr deque))))
(define (front-insert-deque! item)
(let ((new-pair (cons item (cons '() '()))))
(cond ((empty-deque? deque)
(set-front-ptr! deque new-pair)
(set-rear-ptr! deque new-pair)
(print-deque))
(else
(set-cdr! (cdr (front-ptr deque)) new-pair)
(set-car! (cdr new-pair) (front-ptr deque))
(set-front-ptr! deque new-pair)
(print-deque)))))
(define (rear-insert-deque! item)
(let ((new-pair (cons item (cons '() '()))))
(cond ((empty-deque? deque)
(set-front-ptr! deque new-pair)
(set-rear-ptr! deque new-pair)
(print-deque))
(else
(set-car! (cdr (rear-ptr deque)) new-pair)
(set-cdr! (cdr new-pair) (rear-ptr deque))
(set-rear-ptr! deque new-pair)
(print-deque)))))
(define (front-delete-deque!)
(cond ((empty-deque? deque)
(error "DELETE called with an empty deque" deque))
((eq? (front-ptr deque) (rear-ptr deque))
(set-front-ptr! deque '())
(set-rear-ptr! deque '())
(print-deque))
(else
(set-front-ptr! deque (cadr (front-ptr deque)))
(set-cdr! (cdr (front-ptr deque)) '())
(print-deque))))
(define (rear-delete-deque!)
(cond ((empty-deque? deque)
(error "DELETE called with an empty deque" deque))
((eq? (front-ptr deque) (rear-ptr deque))
(set-front-ptr! deque '())
(set-rear-ptr! deque '())
(print-deque))
(else
(set-rear-ptr! deque (cddr (rear-ptr deque)))
(set-car! (cdr (rear-ptr deque)) '())
(print-deque))))
(define (print-deque)
(define (get-deque-iter queue-list ptr)
(if (null? ptr)
queue-list
(get-deque-iter (append queue-list (list (car ptr)))
(cadr ptr))))
(get-deque-iter '() (front-ptr deque)))
(define (dispatch m)
(cond ((eq? m 'rear-insert-deque!) rear-insert-deque!)
((eq? m 'front-insert-deque!) front-insert-deque!)
((eq? m 'rear-delete-deque!) rear-delete-deque!)
((eq? m 'front-delete-deque!) front-delete-deque!)
((eq? m 'front-deque) front-deque)
((eq? m 'rear-deque) rear-deque)
((eq? m 'print-deque) print-deque)
(else (error "Unknown request -- MAKE-DEQUE" m))))
dispatch))
(define d1 (make-deque))
((d1 'rear-insert-deque!) 'a)
((d1 'rear-insert-deque!) 'b)
((d1 'rear-insert-deque!) 'c)