COMPUTER 2430 Object Oriented Programming and Data Structures I
Queue and Stack Queue: First In First Out (FIFO) Stack: Last in First Out (LIFO) In Out
Queue Implementation // Circular array int front, rear, count; front points to where the first element is rear points to where to add next element (not the last element!)
public class Queue { private Object[] items; private int front, rear, count; public Queue( int size ) public boolean isEmpty() public boolean isFull () public void add ( Object x ) throws Exception public Object remove() throws Exception } // class Queue
items = new Object[N]; front = rear = count = 0; Queue Class items = new Object[N]; front = rear = count = 0; front rear 4 (N-1) 1 3 2
Queue Class Add A Add B front front A A 4 (N-1) 1 B 4 (N-1) 1 3 2 rear A A 4 (N-1) 1 B 4 (N-1) 1 3 2 rear 3 2 rear
Queue Class Add C, D Add E front front rear A A 4 (N-1) 1 E B 4 (N-1) rear A A 4 (N-1) 1 E B 4 (N-1) 1 B D C D C 3 2 rear 3 2
Queue Class After a while Remove twice rear rear 4 (N-1) 1 4 (N-1) 1 rear rear F 4 (N-1) 1 4 (N-1) 1 D front front 3 2 3 2
public class Queue <E> { private E[] items; private int front, rear, count; public Queue( int size ) public boolean isEmpty() public boolean isFull () public void add ( E x ) // rear = (rear + 1) % items.length; public E remove() // front = (front + 1) % items.length; }
Queue Method numItems As Implementer // Returns the number of items in the queue public int numItems() { return count; }
User Method numItems // Do not modify the queue public int numItems(Queue q, int qSize) { Queue tq = new Queue(qSize); int num = 0; while ( !q.isEmpty() ) tq.add( q.remove() ); num ++; } while ( !tq.isEmpty() ) q.add( tq.remove() ); return num; Is the 2nd loop necessary?
User Method numItems public int numItems(Queue q, int qSize) ( Queue tq = new Queue(qSize); int num = 0; while ( !q.isEmpty() ) { tq.add( q.remove() ); num ++; } q = tq; // Can we do this? return num; NO! Pass by Value!
User Method numItems public int numItems(Queue q, int qSize) ( Queue tq = new Queue(qSize); int num = 0; while ( !q.isEmpty() ) { tq.add( q.remove() ); num ++; } while ( !tq.isEmpty() ) q.add( tq.remove() ); return num; The 2nd while loop is necessary! Can you do it without tq?
User Method numItems // If array is allowed public int numItems(Queue q, int qSize) ( // Queue tq = new Queue(qSize); Object [] temp = new Object[qSize]; int num = 0; while ( !q.isEmpty() ) { temp[num ++] = q.remove(); } for (int index = 0; index < num; index ++) q.add( temp[index] ); return num;
User Method numItems // If arrays not allowed! public int numItems(Queue q, int qSize) ( Queue tq = new Queue(qSize); int num = 0; while ( !q.isEmpty() ) { tq.add( q.remove() ); num ++; } while ( !tq.isEmpty() ) q.add( tq.remove() ); return num;
Special Queue Operations Remove the second element of the queue Remove the last element of the queue Switch two elements of the queue User vs Implementer
Quizz 4 Monday, November 5 10 points Queue and Exception
Lab 8
Lab 7 Prog 4