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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
(define (make-deque)
(cons '() '()))
(define (front-ptr deque)
(car deque))
(define (rear-ptr deque)
(cdr deque))
(define (set-front-ptr! deque item)
(set-car! deque item))
(define (set-rear-ptr! deque item)
(set-cdr! deque item))
(define (empty-deque? deque)
(null? (front-ptr deque)))
(define (front-deque deque)
(if (empty-deque? deque)
(error "FRONT called with an empty deque"
(display-deque deque))
(car (front-ptr deque))))
(define (rear-deque deque)
(if (empty-deque? deque)
(error "FRONT called with an empty deque"
(display-deque deque))
(car (rear-ptr deque))))
(define (make-pair item front rear)
(cons item (cons front rear)))
(define (front-rear-pair pair)
(cdr pair))
(define (set-pair-front pair front)
(set-car! (front-rear-pair pair) front))
(define (set-pair-rear pair rear)
(set-cdr! (front-rear-pair pair) rear))
(define (pair-item pair)
(car pair))
(define (pair-rear pair)
(cdr (front-rear-pair pair)))
(define (pair-front pair)
(car (front-rear-pair pair)))
(define (front-insert-deque! deque item) ;insert to the front
(let ((new-pair (make-pair item '() '())))
(cond ((empty-deque? deque)
(set-front-ptr! deque new-pair)
(set-rear-ptr! deque new-pair)
(display-deque deque))
(else
(set-pair-front (front-ptr deque) new-pair)
(set-pair-rear new-pair (front-ptr deque))
(set-front-ptr! deque new-pair)
(display-deque deque)))))
(define (rear-insert-deque! deque item) ;insert to the end
(let ((new-pair (make-pair item '() '())))
(cond ((empty-deque?
deque)
(set-rear-ptr! deque new-pair)
(set-front-ptr! deque new-pair)
(display-deque deque))
(else
(set-pair-rear (rear-ptr deque) new-pair)
(set-pair-front new-pair (rear-ptr deque))
(set-rear-ptr! deque new-pair)
(display-deque deque)))))
(define (front-delete-deque! deque) ;delete the front one
(cond ((empty-deque? deque)
(error "DELETE! called with an empty deque"
(display-deque deque)))
(else
(if (eq? (front-ptr deque) (rear-ptr deque))
(begin
(set-front-ptr! deque '())
(set-rear-ptr! deque '()))
(begin
(set-front-ptr! deque (pair-rear (front-ptr deque)))
(set-pair-front (front-ptr deque) '())))
(display-deque deque))))
(define (rear-delete-deque! deque) ;delete the rear one
(cond ((empty-deque? deque)
(error "DELETE! called with an empty deque"
(display-deque deque)))
(else
(if (eq? (front-ptr deque) (rear-ptr deque))
(begin
(set-front-ptr! deque '())
(set-rear-ptr! deque '()))
(begin
(set-rear-ptr! deque (pair-front (rear-ptr deque)))
(set-pair-rear (rear-ptr deque) '())))
(display-deque deque))))
(define (display-deque deque)
(define (fetch-item ptr)
(if (null? ptr)
'()
(cons (pair-item ptr)
(fetch-item (pair-rear ptr)))))
(if (not (empty-deque? deque))
(let ((front (front-ptr deque)))
(fetch-item front))
'()))
|