Presentation is loading. Please wait.

Presentation is loading. Please wait.

ADT Queue (Array Implementation)

Similar presentations


Presentation on theme: "ADT Queue (Array Implementation)"— Presentation transcript:

1 ADT Queue (Array Implementation)

2 ADT Queue (Array Implementation)
Array implementation of the queue…a fixed size array is used to store the data elements. As data elements are enqueued & served the queue crawls through the array from low to high index values. As the queue crawls forward, it also expands and contracts.

3 ADT Queue (Array Implementation)
Head Tail After one En-queue and one Serve Head Tail

4 ADT Queue (Array Implementation)
Head Tail Where to En-queue this?

5 ADT Queue (Array Implementation)
MaxSize-1 Tail Head Wrap Round

6 ADT Queue (Array Implementation)
Tail Head MaxSize - 1

7 Adapted from Pearson Education, Inc.
An empty Queue [0] [1] [2] [3] [4] Front Back 4 Enqueue at back  incr back then store item The queue is EMPTY ( Back + 1 ) % size = ( ) % = 0 == Front = 0 Adapted from Pearson Education, Inc.

8 Adapted from Pearson Education, Inc.
enQueue A [0] A [1] [2] [3] [4] Front Back The queue is not EMPTY ( Back + 1 ) % size = ( ) % = 1 =/= Front = 0 Adapted from Pearson Education, Inc.

9 Adapted from Pearson Education, Inc.
enQueue X [0] A [1] X [2] [3] [4] Front Back 1 The queue is not EMPTY ( Back + 1 ) % size = ( ) % = 2 =/= Front = 0 Adapted from Pearson Education, Inc.

10 enQueue G, B, and Z – it’s full now
Same As When Queue Was empty [0] A [1] X [2] G [3] B [4] Z Front Back 4 Dequeue from Front Serve front then incr front The queue is FULL BUT ( Back + 1 ) % size = ( ) % = 0 == Front = 0 Adapted from Pearson Education, Inc.

11 Adapted from Pearson Education, Inc.
deQueue [0] [1] X [2] G [3] B [4] Z Front 1 Back 4 The queue is not Empty And not full ( Back + 1 ) % size = ( ) % = 0 =/= Front = 1 Adapted from Pearson Education, Inc.

12 Adapted from Pearson Education, Inc.
deQueue [0] [1] [2] G [3] B [4] Z Front 2 Back 4 Let’s enqueue 2 more: W and K The queue is not Empty ( Back + 1 ) % size = ( ) % = 0 =/= Front = 2 Adapted from Pearson Education, Inc.

13 Adapted from Pearson Education, Inc.
enQueue W [0] W [1] [2] G [3] B [4] Z Front 2 Back The queue is not Empty ( Back + 1 ) % size = ( ) % = 1 =/= Front = 2 Adapted from Pearson Education, Inc.

14 enQueue K – it’s full again
[0] W [1] K [2] G [3] B [4] Z Front 2 Back 1 How can we avoid the condition for empty and full being the same? The queue is FULL BUT ( Back + 1 ) % size = ( ) % = 2 == Front = 2 Adapted from Pearson Education, Inc.

15 Start with an empty Queue again
[0] [1] [2] [3] [4] Front Back 4 One Location remains unused The queue is EMPTY ( Back + 1 ) % size = ( ) % = 0 == Front = 0 Adapted from Pearson Education, Inc.

16 enQueue A [0] A [1] [2] [3] [4] Front Back One Location remains unused
Back One Location remains unused The queue is not EMPTY ( Back + 1 ) % size = ( ) % = 1 =/= Front = 0 Adapted from Pearson Education, Inc.

17 enQueue X [0] A [1] X [2] [3] [4] 1 Front Back
Back 1 One Location remains unused The queue is not EMPTY ( Back + 1 ) % size = ( ) % = 2 =/= Front = 1 Adapted from Pearson Education, Inc.

18 enQueue G, and B – it’s full now
Different From When Queue Was empty [0] A [1] X [2] G [3] B [4] Front Back 3 One Location remains unused The queue is FULL ( Back + 2 ) % size = ( ) % = 0 == Front = 0 Adapted from Pearson Education, Inc.

19 deQueue [0] [1] X [2] G [3] B [4] 1 3 Front Back
One Location remains unused The queue is not Empty And not full ( Back + 2 ) % size = ( ) % = 0 =/= Front = 1 Adapted from Pearson Education, Inc.

20 deQueue [0] [1] [2] G [3] B [4] 2 3 Let’s enqueue 2 more: W and K
Front 2 Back 3 One Location remains unused Let’s enqueue 2 more: W and K The queue is not Empty ( Back + 1 ) % size = ( ) % = 4 =/= Front = 2 Adapted from Pearson Education, Inc.

21 enQueue W [0] [1] [2] G [3] B [4] W 2 4 Front Back
One Location remains unused The queue is not Empty ( Back + 1 ) % size = ( ) % = 0 =/= Front = 2 Adapted from Pearson Education, Inc.

22 enQueue K [0] K [1] [2] G [3] B [4] W 2 Let’s enqueue 1 more: Z Front
Back One Location remains unused Let’s enqueue 1 more: Z The queue is not Empty ( Back + 1 ) % size = ( ) % = 1 =/= Front = 2 Adapted from Pearson Education, Inc.

23 enQueue Z – can’t do it… already full
[0] K [1] [2] G [3] B [4] W Front 2 Back One Location remains unused The queue is FULL ( Back + 2 ) % size = ( ) % = 2 == Front = 2 Adapted from Pearson Education, Inc.

24 ADT Queue (Array Implementaion)
public class ArrayQueue <T> { private int maxsize; private int size; private int head, tail; private T[] nodes; /** Creates a new instance of ArrayQueue */ public ArrayQueue(int n) { maxsize = n; size = 0; head = tail = 0; nodes = (T[]) new Object[n]; }

25 ADT Queue (Array Implementation)
public boolean full () { return size == maxsize ? true : false; } public int length () { return size; public void enqueue(T e) { nodes[tail] = e; tail = (tail + 1) % maxsize; size++;

26 ADT Queue -Array Implementation
public T serve () { T e = nodes[head]; head = (head + 1) % maxsize; size--; return e; }


Download ppt "ADT Queue (Array Implementation)"

Similar presentations


Ads by Google