Download presentation
Presentation is loading. Please wait.
Published byΓιάννης Κοντολέων Modified over 6 years ago
1
COMPUTER 2430 Object Oriented Programming and Data Structures I
2
Queue and Stack Queue: First In First Out (FIFO)
Stack: Last in First Out (LIFO) In Out
3
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!)
4
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
5
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
6
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
7
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
8
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
9
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; }
10
Queue Method numItems As Implementer
// Returns the number of items in the queue public int numItems() { return count; }
11
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?
12
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!
13
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?
14
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;
15
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;
16
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
17
Quizz 4 Monday, November 5 10 points Queue and Exception
18
Lab 8
19
Lab 7 Prog 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.