-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
joseph williamon15
committed
Oct 3, 2018
1 parent
abdf7c4
commit 3f79490
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#lang sicp | ||
|
||
(define (make-queue) | ||
(let ((front-ptr '()) | ||
(rear-ptr '())) | ||
(define print | ||
(begin | ||
(display front-ptr) | ||
(newline))) | ||
(define empty? | ||
(null? front-ptr)) | ||
(define (insert item) | ||
(let ((new-pair (cons item '()))) | ||
(cond (empty? | ||
(set! front-ptr new-pair) | ||
(set! rear-ptr new-pair)) | ||
(else (set-cdr! rear-ptr new-pair) | ||
(set! rear-ptr new-pair))))) | ||
(define (dispatch m) | ||
(cond ((eq? m 'print) print) | ||
((eq? m 'empty?) empty?) | ||
((eq? m 'insert) insert) | ||
(else (error "make-queue: undefined operation" m)))) | ||
dispatch)) | ||
|
||
(define (print-queue q) (q 'print)) | ||
(define (empty-queue? q) (q 'empty?)) | ||
(define (insert-queue q i) ((q 'insert) i)) | ||
|
||
(define q1 (make-queue)) | ||
(print-queue q1) | ||
(empty-queue? q1) | ||
(print-queue q1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#lang sicp | ||
|
||
(define (make-queue) | ||
(let ((front-ptr '()) | ||
(rear-ptr '())) | ||
(define (print) | ||
(begin | ||
(display front-ptr) | ||
(newline))) | ||
(define (empty?) | ||
(null? front-ptr)) | ||
(define (front) | ||
(if (empty?) (error "front-queue on empty queue") | ||
(car front-ptr))) | ||
(define (delete) | ||
(cond ((empty?) (error "delete-queue on empty queue")) | ||
(else (set! front-ptr (cdr front-ptr)) | ||
(print)))) | ||
(define (insert item) | ||
(let ((new-pair (cons item '()))) | ||
(cond ((empty?) | ||
(set! front-ptr new-pair) | ||
(set! rear-ptr new-pair)) | ||
(else (set-cdr! rear-ptr new-pair) | ||
(set! rear-ptr new-pair) | ||
(print))))) | ||
(define (dispatch m) | ||
(cond ((eq? m 'print) (print)) | ||
((eq? m 'empty?) (empty?)) | ||
((eq? m 'insert) insert) | ||
((eq? m 'front) (front)) | ||
((eq? m 'delete) (delete)) | ||
(else (error "make-queue: undefined operation" m)))) | ||
dispatch)) | ||
|
||
(define (print-queue q) (q 'print)) | ||
(define (empty-queue? q) (q 'empty?)) | ||
(define (insert-queue q i) ((q 'insert) i)) | ||
(define (front-queue q) (q 'front)) | ||
(define (delete-queue q) (q 'delete)) | ||
|
||
(define q1 (make-queue)) | ||
(print-queue q1) | ||
(empty-queue? q1) | ||
(insert-queue q1 'a) | ||
(front-queue q1) | ||
(empty-queue? q1) | ||
(insert-queue q1 'b) | ||
(insert-queue q1 'c) | ||
(front-queue q1) | ||
(print-queue q1) | ||
(delete-queue q1) | ||
(front-queue q1) | ||
(print-queue q1) | ||
(delete-queue q1) | ||
(front-queue q1) | ||
(print-queue q1) | ||
(delete-queue q1) | ||
(empty-queue? q1) |