Basic queue operations
Queue operations are simple but order-sensitive. Correctly updating front and rear and checking empty/full states is the foundation for every queue implementation in C.
On this page
- Enqueue: insert at rear
- Dequeue: remove from front
- Peek/front and rear inspection
- isEmpty, isFull, and size checks
- Complexity and common implementation mistakes
1) Enqueue (insert)
Enqueue adds a new element at the rear. In a fixed-size array queue, check full condition first to avoid overflow.
- Validate queue is not full.
- Advance/set rear index.
- Store value at rear position.
- If queue was empty, initialize front as well.
2) Dequeue (remove)
Dequeue removes and returns the front element. Always check empty condition first to avoid underflow.
- Validate queue is not empty.
- Read value at front.
- Advance front index (or move front pointer node).
- If last element removed, reset queue to empty state.
3) Peek/front and rear queries
Queue read-only operations inspect values without modifying order:
| Operation | Purpose | Modifies queue? |
|---|---|---|
| front()/peek() | Read next item to dequeue | No |
| rear() | Read most recently enqueued item | No |
| size() | Number of current elements | No |
4) State checks
These utility checks keep enqueue/dequeue safe:
- isEmpty() — true when no elements exist.
- isFull() — true when no insertion space remains (fixed-capacity queues).
- size() — tracks element count for logic and debugging.
Common circular queue checks: empty: size == 0 full: size == capacity rear update: rear = (rear + 1) % capacity front update: front = (front + 1) % capacity
5) Time complexity overview
| Operation | Circular array queue | Linked-list queue |
|---|---|---|
| enqueue | O(1) | O(1) |
| dequeue | O(1) | O(1) |
| front/peek | O(1) | O(1) |
| isEmpty/isFull | O(1) | O(1) |
6) Common mistakes in C code
- Not resetting front/rear when queue becomes empty.
- Checking full condition incorrectly in circular queues.
- Advancing indices before reading values in dequeue.
- Mixing two conventions for rear (last element vs next free slot).
Key takeaway
Queue operations are short but strict: validate state, update the correct end, and keep index conventions consistent. If you do that, enqueue/dequeue remain O(1) and robust.
Next up: Applications of Queue · Queue using array · Circular queue