lecture8 - COMP 250 Winter 2010 lecture 8 queues January...

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

View Full Document Right Arrow Icon
COMP 250 Winter 2010 lecture 8 – queues January 22, 2010 Queues You are familiar with queues in daily life. You know that when you have a single resource such as a cashier in the cafeteria, you need to “join the end of the line” and the person at the front of the line is the one being served. The key property of a queue is that, among those objects/persons/etc currently in the queue, the one being served/removed is the one who frst entered the queue. The queue abstract data type (ADT) has two basic operations associated with it: enqueue(e) which adds an element to the queue, and dequeue() which removes an element from the queue. We could also have operations isEmpty() which checks if there is an element in the queue, and front() which returns the Frst element in the queue (but does not remove it), and capacity which returns the number of items in the queue. Often one writes add and remove instead of enqueue and dequeue . 1 Notice that these opera- tions are basically the same as those of a stack, except that their meaning is di±erent: removing an element from a queue removes the least recently added element, whereas removing an element from a stack removes the most recently added. We say that queues implement “Frst come, Frst served” policy (also called ²I²O, Frst in Frst out), whereas stacks implement a LI²O policy, namely last in, Frst out. Example Suppose we add (and remove) items a,b,c,d,e,f,g in the following order, shown on left. On the right is show the corresponding state of the queue aFter the operation. OPERATION STATE - add(a) a add(b) ab remove() b add(c) bc add(d) bcd add(e) bcde remove() cde add(f) cdef remove() def add(g) defg Data structures for implementing a queue Singly linked list One way to implement a queue is with a singly linked list. Just as you join a line at the back, when you add an element to a singly linked list queue, you manipulate the tail reference. This can be done by modifying two references, namely the tail reference and the next reference of the 1 The reason is presumably because it is somewhat awkward to say “we enqueue the element” or “we dequeue an
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/25/2011 for the course COMP 250 taught by Professor Blanchette during the Spring '08 term at McGill.

Page1 / 4

lecture8 - COMP 250 Winter 2010 lecture 8 queues January...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online