rear-delete-deque.scm (1200B)
1 (define-library (sicp solutions chapter-3 exercise-23-stuff rear-delete-deque) 2 (import (scheme base)) 3 4 (import (sicp solutions chapter-3 exercise-23-stuff empty-deque)) 5 (import (sicp solutions chapter-3 exercise-23-stuff front-ptr)) 6 (import (sicp solutions chapter-3 exercise-23-stuff rear-ptr)) 7 (import (sicp solutions chapter-3 exercise-23-stuff set-front-ptr)) 8 (import (sicp solutions chapter-3 exercise-23-stuff set-rear-ptr)) 9 (import (prefix (sicp solutions chapter-3 exercise-23-stuff doubly-linked-list) dll:)) 10 11 (export rear-delete-deque!) 12 13 (begin 14 (define (rear-delete-deque! deque) 15 "Delete rearmost item of DEQUE if DEQUE is not empty, otherwise raise an error." 16 (cond 17 ((empty-deque? deque) 18 (error "REAR-DELETE-DEQUE! called with an empty deque" deque)) 19 ((eq? (dll:link-rear (front-ptr deque)) 20 '()) ; The front link's rear pointer would point at '() only if it is the only link in the DEQUE. 21 (set-front-ptr! deque '()) 22 (set-rear-ptr! deque '())) 23 (else 24 (set-rear-ptr! deque 25 (dll:link-front (rear-ptr deque))) 26 (dll:delete-rear! (rear-ptr deque)))) 27 deque)))