FIFOs, First-In-First-Out
enqueue(x)
dequeue
is_empty()
If try to dequeue from an empty queue, will get EmptyQueueException
.
Whenever enqueue
(resp. dequeue
) we increment rear
(resp. front
).
pos = (pos + 1) % 6
To generalise:
pos = (pos + 1) % size
var queue = new int[MAXQUEUE];
int front = 0;
int size = 0;
-
font + size > MAXQUEUE
pos
∈ [font, MAXQUEUE) ∪ [0, front + size - MAXQUEUE) -
font
∈ [0, MAXQUEUE) -
size
∈ [0, MAXQUEUE]
rear = (front + size) % MAXQUEUE
if size == MAXQUEUE
, the queue is full.
- Is not full?
- Insert data into index
(front + size) % MAXQUEUE
- Add 1 to
size
- Is not empty?
- Get value at index
front
front = (front + 1) % MAXQUEUE
- Minus 1 to
size