Cis120 spring 2012 7 enq add an element to the tail

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (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! •  We can check that these proper@es rule out “bogus” examples. •  A queue opera@on 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 enq! (* 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! •  The code for enq is informed by the queue invariant: –  either the...
View Full Document

Ask a homework question - tutors are online