Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I

2 Queue Container Normally fixed size (could grow if needed)
FIFO: First In First Out Example Mongolian Grill Printing jobs Network routing . . .

3 Queue Implementation An array Elements are added at the end
Elements are removed from the front

4 Queue Empty queue Add item1 Item1 Add item2 Item1 Item2 Add item3
Remove the first one Item2 Item3 Add item4 Item2 Item3 Item4

5 First In First Out (FIFO) In at one end and Out at the other end
Queue First In First Out (FIFO) In at one end and Out at the other end Out In

6 Public Queue Methods add (at the end) remove (from the front) isEmpty
isFull

7 Queue Implementation I
Add is easy Remove has to move all remaining elements Remove is always from slot 0 count also indicates where next element goes

8 Queue Implementation II
Add at the end and moves rear Remove at the front and moves pointer front Rear is not count any more! front points to where the first element is rear points to where next element goes

9 Queue Implementation II
Very efficient Need to keep count Trouble when hit the end of array front points to where the first element is rear points to where next element goes

10 Queue Implementation III
Make the array circular Need to keep count front points to where the first element is rear points to where next element goes

11 front rear count: 0 Add . . . front rear count: 7 Remove front rear count: 6 Add && Remove front rear count: 8 Add rear front count: 9

12 rear front count: 9 Add rear front count: 10 Remove rear front count: 9 Add && Remove rear front count: 6 Remove front rear count: 5

13 Full: count == items.length Empty: count == 0
Add && Remove front rear count: 8 Add . . . front rear count: 12 Add rear front count: 13 Add front rear count: 14 Full: count == items.length Empty: count == 0

14 Add Check isFull first or Throw Exception items[rear ++] = obj;
front rear Add Check isFull first or Throw Exception front rear rear front rear front items[rear ++] = obj; if (rear == items.length) rear = 0; count ++; items[rear] = obj; rear = (rear + 1) % items.length; count ++;

15 Remove Check isEmpty first or Throw Exception obj = items[front ++];
rear front rear front front rear front rear obj = items[front ++]; if (front == items.length) front = 0; count --; obj = items[front]; front = (front + 1) % items.length; count --;

16 public class Queue { private Object[] items; private int front, rear, count; public Queue( int size ) items = new Object[size]; front = rear = count = 0; } public boolean isEmpty() return count == 0; public boolean isFull () return count == items.length; ... } // class Queue

17 public class Queue { private Object[] items; private int front, rear, count; ... // what if queue is full? public void add ( Object x ) items[rear] = x; rear = (rear + 1) % items.length; count ++; } // what if queue is empty? public Object remove() Object x = items[front]; front = (front + 1) % items.length; count --; return x; } // class Queue

18 // Generic Queue class public class Queue <E> { private E[] items; private int front, rear, count; // Java warning possible and it’s OK public Queue( int size ) items = (E[]) new Object[size]; front = rear = count = 0; } . . .

19 Due October 31, at 11 pm No Grace Date!
Lab 8 Due October 31, at 11 pm No Grace Date!

20 Schedule Lab 7 (5points) Prog4 (20 points) Quiz 4 Test 2
Due 11 pm, November 2 Prog4 (20 points) Due 11 pm, November 9 Quiz 4 Monday, Nov 5 Test 2 Wednesday, Nov 14


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google