If the removed node was the last one in the queue the

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: 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 deq! (* 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! •  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 Queue Length •  Suppose we want to extend...
View Full Document

Ask a homework question - tutors are online