1 / 19

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

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

kbhakta
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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. 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

  5. 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

  6. 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

  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 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 ?

  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. 1 2 3 q ()

  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>)) (if (empty-queue? q) (error "Cannot delete from empty queue") (set! (head q) (tail (head q)))) 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. (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: Insert: add at headO(1) Delete: search for minO(n) Next time: Insert:O(log n) Delete:O(log n)

More Related