--- /dev/null
+#lang r5rs
+
+(define error display)
+
+(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 on an empty queue" queue))
+ (else
+ (set-front-ptr! queue (cdr (front-ptr queue)))
+ queue)))
+
+(define (print-queue queue)
+ (define (copy-queue q1)
+ (let ((q2 (make-queue)))
+ (set-front-ptr! q2 (front-ptr q1))
+ (set-rear-ptr! q2 (rear-ptr q1))
+ q2))
+ (let ((q (copy-queue queue)))
+ (if (not (empty-queue? q))
+ (begin
+ (display (front-queue q))
+ (display " ")
+ (print-queue (delete-queue! q))))))
+
+
+
\ No newline at end of file
--- /dev/null
+#lang r5rs
+
+(define error display)
+(define (make-queue)
+ (let ((front-ptr '())
+ (rear-ptr '()))
+ (define (set-front-ptr! queue item)
+ (set! front-ptr item))
+ (define (set-rear-ptr! queue item)
+ (set! rear-ptr item))
+ (define (empty-queue? queue)
+ (null? front-ptr))
+ (define (front-queue queue)
+ (if (empty-queue? queue)
+ (error "FRONT called with an empty queue" queue)
+ (car front-ptr)))
+ (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)
+ front-ptr)
+ (else
+ (set-cdr! rear-ptr new-pair)
+ (set-rear-ptr! queue new-pair)
+ front-ptr))))
+ (define (delete-queue! queue)
+ (cond ((empty-queue? queue)
+ (error "DELETE! called with an empty queue" queue))
+ (else
+ (set-front-ptr! queue (cdr front-ptr))
+ front-ptr)))
+ (define (dispatch m)
+ (cond
+ ((eq? m 'insert-queue!)
+ (lambda (queue item)
+ (insert-queue! queue item)))
+ ((eq? m 'delete-queue!)
+ (lambda (queue)
+ (delete-queue! queue)))
+ ((eq? m 'empty-queue?)
+ (lambda (queue)
+ (empty-queue? queue)))
+ ((eq? m 'front-queue)
+ (lambda (queue)
+ (front-queue queue)))))
+ dispatch))
+
+(define (empty-queue? queue)
+ ((queue 'empty-queue?) queue))
+
+(define (front-queue queue)
+ ((queue 'front-queue) queue))
+
+(define (insert-queue! queue item)
+ ((queue 'insert-queue!) queue item))
+
+(define (delete-queue! queue)
+ ((queue 'delete-queue!) queue))