-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.rkt
42 lines (34 loc) · 1.15 KB
/
queue.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
#lang racket
(require compatibility/mlist)
(define (make-queue) (mcons '() '()))
(define (empty-queue? queue) (null? (front-ptr queue)))
(define (front-queue queue)
(if (empty-queue? queue)
(error "FRONT called with an empty queue" queue)
(mcar (front-ptr queue))))
(define (insert-queue! queue item)
(let ((new-pair (mcons item '())))
(cond ((empty-queue? queue)
(set-front-ptr! queue new-pair)
(set-rear-ptr! queue new-pair)
'ok)
(else
(set-mcdr! (rear-ptr queue) new-pair)
(set-rear-ptr! queue new-pair)
'ok))))
(define (delete-queue! queue)
(cond ((empty-queue? queue)
(error "DELETE! called with an empty queue" queue))
(else
(set-front-ptr! queue (mcdr (front-ptr queue)))
'ok)))
(define (print-queue queue)
(mlist->list (front-ptr queue)))
(define (front-ptr queue) (mcar queue))
(define (rear-ptr queue) (mcdr queue))
(define (set-front-ptr! queue item) (set-mcar! queue item))
(define (set-rear-ptr! queue item) (set-mcdr! queue item))
(define q (make-queue))
(insert-queue! q 'a)
(insert-queue! q 'b)
(insert-queue! q 'c)