Linear Data Structures 1.9

# Linear Data Structures 1.9 - Queue Example Operation Q...

This preview shows pages 1–5. Sign up to view the full content.

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

View Full Document

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

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

Unformatted text preview: Queue Example Operation Q enqueue(5) – (5) enqueue(3) – (5, 3) dequeue() 5 (3) enqueue(7) – (3, 7) dequeue() 3 (7) front() 7 (7) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue(9) – (9) enqueue(7) – (9, 7) size() 2 (9, 7) enqueue(3) – (9, 7, 3) enqueue(5) – (9, 7, 3, 5) dequeue() CSE 2011 Prof. J. Elder Output 9 (7, 3, 5) - 41 - Last Updated: 12-01-17 9:52 AM Array-Based Queue •  Use an array of size N in a circular fashion •  Two variables keep track of the front and rear f index of the front element r index immediately past the rear element •  Array location r is kept empty normal configuration Q 012 f r wrapped-around configuration Q 012 CSE 2011 Prof. J. Elder r f - 42 - Last Updated: 12-01-17 9:52 AM Queue Operations •  We use the modulo operator (remainder of division) Algorithm size() return (N - f + r) mod N Algorithm isEmpty() return (f = r) Note: N ! f + r = (r + N ) ! f Q 012 f 012 r r Q CSE 2011 Prof. J. Elder f - 43 - Last Updated: 12-01-17 9:52 AM Queue Operations (cont.) •  Operation enqueue may throw an exception if the array is full Algorithm enqueue(o) if size() = N - 1 then throw FullQueueException else Q[r]  o r  (r + 1) mod N Q 012 f 012 r r Q CSE 2011 Prof. J. Elder f - 44 - Last Updated: 12-01-17 9:52 AM Queue Operations (cont.) •  Operation dequeue throws an exception if the queue is empty Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o Q 012 f 012 r r Q CSE 2011 Prof. J. Elder f - 45 - Last Updated: 12-01-17 9:52 AM ...
View Full Document

## This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.

### Page1 / 5

Linear Data Structures 1.9 - Queue Example Operation Q...

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

View Full Document
Ask a homework question - tutors are online