{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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]
Image of page 1

Info icon This 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
Image of page 2
Queues singly-­‐linked
Image of page 3

Info icon This 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
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
Image of page 5

Info icon This 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
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
Image of page 7

Info icon This 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
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
Image of page 9

Info icon This 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?
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern