190 likes | 196 Views
1. 2. 3. Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4))) (set! (tail new) (tail mylist)) (set! (tail mylist) new)). (). mylist. 4. 1. 2. 3. Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4))) (set! (tail new) (tail mylist))
E N D
1 2 3 • Mutable Data • (define mylist (list 1 2 3)) • (bind ((new (list 4))) • (set! (tail new) (tail mylist)) • (set! (tail mylist) new)) () mylist
4 1 2 3 • Mutable Data • (define mylist (list 1 2 3)) • (bind ((new (list 4))) • (set! (tail new) (tail mylist)) • (set! (tail mylist) new)) () mylist () new
4 1 2 3 • Mutable Data • (define mylist (list 1 2 3)) • (bind ((new (list 4))) • (set! (tail new) (tail mylist)) • (set! (tail mylist) new)) () mylist () new X
4 1 2 3 • Mutable Data • (define mylist (list 1 2 3)) • (bind ((new (list 4))) • (set! (tail new) (tail mylist)) • (set! (tail mylist) new)) () X mylist () new
4 1 2 3 • Mutable Data • (define mylist (list 1 2 3)) • (bind ((new (list 4))) • (set! (tail new) (tail mylist)) • (set! (tail mylist) new)) () mylist () new
4 1 2 3 • Mutable Data • (define mylist (list 1 2 3)) • (bind ((new (list 4))) • (set! (tail new) (tail mylist)) • (set! (tail mylist) new)) () mylist
Stacks and Queues • stacks: • last-in-first-out (LIFO) • queues: • first-in-first-out (FIFO)
Stacks • (make-stack) • makes a new empty stack • (push thing stack) • returns a new stack with thing on top ofstack • (pop stack) • returns a stack like stack, but without its top element • (top stack) • returns the top element of stack • (empty? stack) • Is there anything on the stack ?
Contract for Stacks (top (push thing stack)) = thing (pop (push thing stack)) = stack (empty? (make-stack)) = #t (empty? (push thing stack)) = #f
Implement Stacks with Lists push = pair pop = tail top = head empty? = null? All operations are O(1) time
Queues • (make-queue) • makes a new empty queue • (insert thing queue) • returns a new queue with thing as last element • (delete queue) • returns a queue like queue, but without its first element • (head queue) • returns the first element of queue • (empty? queue) • tests emptiness
Implementation of Queues as Lists head = head delete = tail empty? = null? Insert: (method ((thing <object>) (q <list>)) (append q (list thing))) Insert is O(n) time
1 2 3 q ()
(define <queue> <pair>) (define (empty-queue? <function>) (method ((q <queue>)) (null? (head q)))) (define (make-queue <function>) (method () '(()))) (define (queue-head <function>) (method ((q <queue>)) (if (empty-queue? q) (error ”Cannot take head of empty queue") (head (head q)))))
(define (insert <function>) (method ((x <object>) (q <queue>)) (bind (((new <list>) (list x))) (if (empty-queue? q) (set! (head q) new) (set! (tail (tail q)) new)) (set! (tail q) new)) q)) (define (delete <function>) (method ((q <queue>)) (if (empty-queue? q) (error "Cannot delete from empty queue") (set! (head q) (tail (head q)))) q))
Priority Queues (define-class <entry> (<object>) (key <integer>) (data <object>)) (define <priority-queue> <list>) ;;make a new queue entry with key n and data d (define (make-entry <function>) (method ((n <integer>) (d <object>)) (make <entry> key: n data: d)))
(define (insert-1 <function>) (method ((e <entry>) (q <priority-queue>)) (cond ((null? q) (list e)) ((< (key e) (key (head q))) (pair e q)) (else: (pair (head q) (insert-1 e (tail q))))))) To insert: (set! *q* (insert-1 e *q*)) Insert: O(n) Delete: O(1)
(define insert-2 (method ((e <entry>) (q <priority-queue>)) ;check if new element should go at head of list (if (or (null? q) (< (key e) (key (head q)))) (pair e q) ;no: find element that it goes immediately after (bind-methods ((find-place ((q <priority-queue>)) (if (or (null? (tail q)) (< (key e) (key (second q)))) q (find-place (tail q))))) (bind ((pq (find-place q)) (le (pair e (tail pq)))) ;link new element in (set! (tail pq) le) ;return original list q)))))
Alternatively: Insert: add at headO(1) Delete: search for minO(n) Next time: Insert:O(log n) Delete:O(log n)