ADT Queue (Array Implementation)
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.
ADT Queue (Array Implementation) Head Tail After one En-queue and one Serve Head Tail
ADT Queue (Array Implementation) Head Tail Where to En-queue this?
ADT Queue (Array Implementation) MaxSize-1 Tail Head Wrap Round
ADT Queue (Array Implementation) Tail Head MaxSize - 1
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 = ( 4 + 1 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.
Adapted from Pearson Education, Inc. enQueue A [0] A [1] [2] [3] [4] Front Back The queue is not EMPTY ( Back + 1 ) % size = ( 0 + 1 ) % 5 = 1 =/= Front = 0 Adapted from Pearson Education, Inc.
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 = ( 1 + 1 ) % 5 = 2 =/= Front = 0 Adapted from Pearson Education, Inc.
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 = ( 4 + 1 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.
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 = ( 4 + 1 ) % 5 = 0 =/= Front = 1 Adapted from Pearson Education, Inc.
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 = ( 4 + 1 ) % 5 = 0 =/= Front = 2 Adapted from Pearson Education, Inc.
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 = ( 0 + 1 ) % 5 = 1 =/= Front = 2 Adapted from Pearson Education, Inc.
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 = ( 1 + 1 ) % 5 = 2 == Front = 2 Adapted from Pearson Education, Inc.
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 = ( 4 + 1 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.
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 = ( 0 + 1 ) % 5 = 1 =/= Front = 0 Adapted from Pearson Education, Inc.
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 = ( 1 + 1 ) % 5 = 2 =/= Front = 1 Adapted from Pearson Education, Inc.
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 = ( 3 + 2 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.
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 = ( 3 + 2 ) % 5 = 0 =/= Front = 1 Adapted from Pearson Education, Inc.
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 = ( 3 + 1 ) % 5 = 4 =/= Front = 2 Adapted from Pearson Education, Inc.
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 = ( 4 + 1 ) % 5 = 0 =/= Front = 2 Adapted from Pearson Education, Inc.
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 = ( 0 + 1 ) % 5 = 1 =/= Front = 2 Adapted from Pearson Education, Inc.
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 = ( 0 + 2 ) % 5 = 2 == Front = 2 Adapted from Pearson Education, Inc.
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]; }
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++;
ADT Queue -Array Implementation public T serve () { T e = nodes[head]; head = (head + 1) % maxsize; size--; return e; }