-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.scm
51 lines (39 loc) · 1.26 KB
/
queue.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
;;SICP 3.3.2
(define (front-ptr queue) (car queue))
(define (rear-ptr queue) (cdr queue))
(define (set-front-ptr! queue item) (set-car! queue item))
(define (set-rear-ptr! queue item) (set-cdr! queue item))
(define (empty-queue? queue) (null? (front-ptr queue)))
(define (make-queue) (cons '() '()))
(define (front-queue queue)
(if (empty-queue? queue)
(error "FRONT called with an empty queue" queue)
(car (front-ptr queue))))
(define (insert-queue! queue item)
(let ((new-pair (cons item '())))
(cond ((empty-queue? queue)
(set-front-ptr! queue new-pair)
(set-rear-ptr! queue new-pair)
queue)
(else
(set-cdr! (rear-ptr queue) new-pair)
(set-rear-ptr! queue new-pair)
queue))))
(define (delete-queue! queue)
(cond ((empty-queue? queue)
(error "DELETE! called with an empty queue" queue))
(else
(set-front-ptr! queue (cdr (front-ptr queue)))
queue)))
;;SICP Exercise 3.21
(define q1 (make-queue))
;expected: ( ( a b ) ( a b ) ( b ) () )
(insert-queue! q1 'a)
(define s1 (front-ptr q1))
(insert-queue! q1 'b)
(define s2 (front-ptr q1))
(delete-queue! q1)
(define s3 (front-ptr q1))
(delete-queue! q1)
(define s4 (front-ptr q1))
(list s1 s2 s3 s4)