{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec15 - Programming Languages and Techniques(CIS120 Lecture...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming Languages and Techniques (CIS120) Lecture 15 Oct 8, 2012 [email protected]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Announcements Homework 5 (queues) are on the web It is due Tuesday, October 16 th at 11:59:59pm Midterm 1 will be in class on Friday, October 12 th ROOMS: Towne 100 (here) last names: A – L Annenberg School 110 last names: M – Z TIME: 11:00a.m. sharp Covers up to Oct 1 st no mutable records, no Abstract Stack Machine CIS120/ Spring 2012
Background image of page 2
Queues singly-­‐linked
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Queue Interface CIS120 / Spring 2012 module type QUEUE = sig (* type of the data structure *) type 'a queue (* Make a new, empty queue *) val create : unit -> 'a queue (* Determine if the queue is empty *) val is_empty : 'a queue -> bool (* Add a value to the tail of the queue *) val enq : 'a -> 'a queue -> unit (* Remove the head value and return it (if any) *) val deq : 'a queue -> 'a end 4
Background image of page 4
Datatypes for Mutable Queues CIS120 / Spring 2012 type 'a qnode = { v : 'a ; mutable next : 'a qnode option } type 'a queue = { mutable head : 'a qnode option ; mutable tail : 'a qnode option } There are two parts to a mutable queue: -­‐ the “internal nodes” of the queue with links from one to the next -­‐ the head and tail references themselves 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example Queues in the Heap CIS120 / Spring 2012 6 head tail An empty queue head tail v 1 next A queue with one element A queue with two elements head tail v 1 next v 2 next None means Some Val Val means
Background image of page 6
Queue Invariants Just as we imposed some [email protected] on which trees are [email protected] Binary Search Trees, Queues must also [email protected] some invariants : We can check that these [email protected] rule out “bogus” examples. A queue [email protected] may assume that these queue invariants hold of its inputs, so long as it ensures that the invariants hold when it’s done. CIS120 / Spring 2012 7 Either: (1) head and tail are both None (i.e. the queue is empty) or (2) head is Some n1 , tail is Some n2 and -­‐ n2 is reachable from n1 by following ‘next’ pointers -­‐ n2.next is None
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
enq The code for enq is informed by the queue invariant: either the queue is empty, and we just update head and tail, or the queue is non-­‐empty, in which case we have to “patch up” the “next” link of the old tail node to maintain the queue invariant. CIS120 / Spring 2012 8 (* add an element to the tail of a queue *) let enq ( x : 'a ) ( q : 'a queue ) : unit = let newnode = { v = x ; next = None } in begin match q . tail with | None -> (* Note that the invariant tells us that q.head is also None *) q . head <- Some newnode ; q . tail <- Some newnode | Some n -> n . next <- Some newnode ; q . tail <- Some newnode end
Background image of page 8
deq The code for deq must also “patch pointers” to maintain the queue invariant: The head pointer is always updated to the next element in the queue. If the removed node was the last one in the queue, the tail pointer must be updated to None CIS120/ Spring 2012 (* remove an element from the head of the queue *) let deq ( q : 'a queue ) : 'a = begin match q . head with | None -> failwith "deq called on empty queue" | Some n -> q . head <- n . next ; if n . next = None then q . tail <- None ; n . v end
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Queue Length Suppose we want to extend the interface with a length [email protected]: How can we implement it?
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}