One sometimes inconvenient feature of lists in Scheme is that it is only possible to iterate through the list in one direction. We can get around this by implementing two-way lists. Consider that regular lists are built from cells (i.e., cons cells) containing two pointers, where the first (head) points to a value of the list and the second (tail) points to the next cell. We can extend this idea to handle two-way lists by adding an extra pointer to our list element. Consider the following code:
(defclass <triple> () (previous :type <top> :initarg :prev) (value :type <top> :initarg :val) (next :type <top> :initarg :next)) (define (make-triple p v n) (make <triple> :prev p :val v :next n))
Using this code, we can now implement a mutable double ended queue (dequeue) which supports the following operations such that they take O(1) (constant) time.
The implementation should behave as follows:
(define (a <deque>) (new-mutable-deque)) (define (b <deque>) (new-mutable-deque)) (insert-front! 5 a) (insert-front! 4 a) (insert-end! 'fred b) (insert-end! 7 a) (insert-front! 'barney b) (extract-front! a) --> 4 (insert-end! 'beavis b) (extract-end! a) --> 7 (insert-front! 2 a) (extract-end! a) --> 5 (extract-end! a) --> 2 (extract-front! b) --> barney (extract-front! b) --> fred (extract-front! b) --> beavis
(defclass <deque> (define new-mutable-queue (method ((o <top>) (q <deque>)) (define insert-front! (method ((o <top>) (q <deque>)) (define extract-end! (method ((q <deque>))