-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.rkt
62 lines (53 loc) · 1.82 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#lang racket
; (require rnrs/mutable-pairs-6)
(define (make-queue)
; pointers to first and last pair in queue (null when default)
(define first '())
(define last '())
; whether or not the queue is empty
(define (empty?)
(null? first))
; the number of items in the queue
(define (size)
; there is no mlength :/
(define (iter mlist count)
(if (null? mlist) count
(iter (mcdr mlist) (+ count 1))))
(iter first 0))
; adds item to end of queue
(define (add! item)
(if (empty?)
; if empty, make a new list and make both pointer point to it
(let ((new-queue (mcons item '())))
(set! first new-queue)
(set! last new-queue))
; else, make the cdr at the end of the queue hold a new pair
; then point the last pair pointer to the new pair
(begin
(set-mcdr! last (mcons item '()))
(set! last (mcdr last)))))
; the item at front of queue
(define (peek)
(mcar first))
; remove and return the item at front of queue
(define (remove!)
(let ((removed (peek)))
; point first pair pointer to next pair
(set! first (mcdr first))
; if first pair pointer is null, the queue must be empty,
; so set last pair pointer to null too
(when (null? first)
(set! last '()))
removed))
; allow methods to be accessed using (queue-instance 'method-name)
(define (dispatch method)
(cond ((equal? method 'empty?) empty?)
((equal? method 'size) size)
((equal? method 'add!) add!)
((equal? method 'peek) peek)
((equal? method 'remove!) remove!)
(else (error (string-append "Method "
(symbol->string method)
" doesn't exist")))))
dispatch)
(provide make-queue)