Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))

Similar presentations


Presentation on theme: "Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))"— Presentation transcript:

1 Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
(set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist () 1 2 3

2 Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
(set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist () 1 2 3 new () 4

3 Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
(set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist () 1 2 3 new X () 4

4 Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
(set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist X () 1 2 3 new () 4

5 Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
(set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist () 1 2 3 new () 4

6 Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
(set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist () 1 2 3 4

7 Stacks and Queues stacks: last-in-first-out (LIFO) queues:
first-in-first-out (FIFO)

8 Stacks (make-stack) makes a new empty stack (push thing stack)
returns a new stack with thing on top of stack (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 ?

9 Contract for Stacks (top (push thing stack)) = thing
(pop (push thing stack)) = stack (empty? (make-stack)) = #t (empty? (push thing stack)) = #f

10 Implement Stacks with Lists
push = pair pop = tail top = head empty? = null? All operations are O(1) time

11 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

12 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

13 q () 1 2 3

14 (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)))))

15 (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>)) (error "Cannot delete from empty queue") (set! (head q) (tail (head q))))

16 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)))

17 To insert: (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)

18 (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)))))

19 Alternatively: Next time: Insert: add at head O(1)
Delete: search for min O(n) Next time: Insert: O(log n) Delete: O(log n)


Download ppt "Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))"

Similar presentations


Ads by Google