111 lines
3.0 KiB
Scheme
111 lines
3.0 KiB
Scheme
|
(load "util.scm")
|
||
|
|
||
|
(display "\nexample - queue\n")
|
||
|
|
||
|
(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)))
|
||
|
|
||
|
(define q (make-queue))
|
||
|
(insert-queue! q 'a)
|
||
|
(insert-queue! q 'b)
|
||
|
(delete-queue! q)
|
||
|
(insert-queue! q 'c)
|
||
|
(insert-queue! q 'd)
|
||
|
(delete-queue! q)
|
||
|
|
||
|
(display "\nex-3.21 - print queue\n")
|
||
|
|
||
|
(define print-queue front-ptr)
|
||
|
|
||
|
(define q1 (make-queue))
|
||
|
(assert (print-queue (insert-queue! q1 'a)) '(a))
|
||
|
(assert (print-queue (insert-queue! q1 'b)) '(a b))
|
||
|
(assert (print-queue (delete-queue! q1)) '(b))
|
||
|
(assert (print-queue (delete-queue! q1)) '())
|
||
|
|
||
|
|
||
|
(display "\nex-3.22 - procedural queue\n")
|
||
|
|
||
|
(define (make-queue)
|
||
|
(let ((front-ptr '())
|
||
|
(rear-ptr '()))
|
||
|
(define (empty-queue?) (null? front-ptr))
|
||
|
(define (insert-queue! item)
|
||
|
(let ((new-pair (cons item '())))
|
||
|
(cond ((empty-queue?)
|
||
|
(set! front-ptr new-pair)
|
||
|
(set! rear-ptr new-pair)
|
||
|
dispatch)
|
||
|
(else
|
||
|
(set-cdr! rear-ptr new-pair)
|
||
|
(set! rear-ptr new-pair)
|
||
|
dispatch))))
|
||
|
(define (delete-queue!)
|
||
|
(cond ((empty-queue?)
|
||
|
(error "DELETE! called with an empty queue" queue))
|
||
|
(else
|
||
|
(set! front-ptr (cdr front-ptr))
|
||
|
dispatch)))
|
||
|
(define (dispatch m)
|
||
|
(cond
|
||
|
((eq? m 'insert) insert-queue!)
|
||
|
((eq? m 'delete) delete-queue!)
|
||
|
((eq? m 'print) (lambda () front-ptr))
|
||
|
(else (error "Undefined operation -- DISPATCH-QUEUE" m))))
|
||
|
dispatch)))
|
||
|
|
||
|
(define q (make-queue))
|
||
|
((q 'insert) 'a)
|
||
|
((q 'insert) 'b)
|
||
|
((q 'insert) 'c)
|
||
|
((q 'insert) 'd)
|
||
|
(assert ((q 'print)) '(a b c d))
|
||
|
((q 'delete))
|
||
|
((q 'delete))
|
||
|
(assert ((q 'print)) '(c d))
|
||
|
|
||
|
(display "\nex-3.23 - deque\n")
|
||
|
|
||
|
; A deque (``double-ended queue'') is a sequence in which items can be inserted
|
||
|
; and deleted at either the front or the rear. Show how to represent deques
|
||
|
; using pairs, and give implementations of the operations.23 All operations
|
||
|
; should be accomplished in (1) steps.
|
||
|
|
||
|
; make-deque
|
||
|
; empty-deque?
|
||
|
; front-deque
|
||
|
; rear-deque
|
||
|
; front-insert-deque!
|
||
|
; rear-insert-deque!
|
||
|
; front-delete-deque!
|
||
|
; rear-delete-deque!
|
||
|
|