notes16 - Queues Data structure where the insert and remove...

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

View Full Document Right Arrow Icon
Queues Data structure where the insert and remove routines follow the "first-in-first- out" (FIFO) principle. The element that has been in the queue the longest is the one that is removed next. Elements are inserted at the front of the queue and removed from the tail . Example: An array-based queue, also called a "circular array," is an array of N elements and two integer variables: front and tail . 0 1 2 3 4 5 6 7 8 9 X Y Z A B front tail At some point, front and tail will wrap around. .. 0 1 2 3 4 5 6 7 8 9 A B X Y Z tail front Implementation (Java): class Queue { int capacity; int currentSize; Object queue[]; int front, tail; public Queue (int capacity) { this.capacity = capacity; currentSize = 0; queue = new Object[capacity]; front = tail = 0; } public void enqueue(Object o) throws QueueFullException { if (currentSize == capacity) { // Queue is full throw new QueueFullException(); } // Queue has space queue[tail] = o; tail = (tail + 1) % capacity; // Wrap-around ability currentSize++;
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 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 3

notes16 - Queues Data structure where the insert and remove...

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