Queue, circular queue and deque
Queue, circular queue and deque: ADT definitions, array/linked implementations, circular index arithmetic, operations, complexities, C++ examples and exam-focused notes.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Queues and deques are linear abstract data types used everywhere in engineering: task scheduling, IO buffering, breadth-first search (BFS), producer–consumer problems and many streaming algorithms. Knowing array and circular implementations and deque operations lets you implement efficient buffers and scheduling structures required in OS, networks and applications.
Key ideas
enqueue(x): insert element x at the rear (also called tail).
dequeue(): remove and return the element at the front (also called head).
front()/peek(): return (without removing) the element at the front.
isEmpty(), isFull() (if bounded). State invariant: after any sequence of operations the order of elements remains in FIFO order.
Bounded / Unbounded queue: a bounded queue has a fixed capacity N; an unbounded queue grows (e.g., using linked nodes) until memory limits.
Circular queue (array implementation): a bounded queue implemented with a fixed-size array of length N that reuses freed positions by wrapping indices modulo N. Key arithmetic: to move an index i one step ahead use (i + 1) mod N. Implementation usually keeps either a pair (front, rear) plus a count, or front and rear where one slot is kept empty to distinguish full from empty. Using a count avoids the wasted-slot approach.
Draw (for exams): draw a horizontal row of N boxes labeled 0,1,...,N-1. Write values in boxes. Draw an arrow labeled "front ->" pointing to the index of the front element, and another arrow labeled "rear ->" pointing to the index of the last element. If empty, draw front and rear values as front=0, rear=-1 or show count=0. For wrap-around show rear near index 0 and front near a larger index.
Array circular queue variables (one common scheme):
- arr[0..N-1]: storage array.
- front: index of current front element (valid only if count>0).
- rear: index of current rear element (valid only if count>0).
- count: number of elements (0..N). Invariant: 0 <= front < N, -1 <= rear < N, 0 <= count <= N and when count==0 queue is empty. If count > 0 then rear = (front + count - 1) % N.
Algorithms (stepwise):
- enqueue(x): if count == N then overflow; else rear = (rear + 1) mod N; arr[rear] = x; count = count + 1.
- dequeue(): if count == 0 then underflow; else val = arr[front]; front = (front + 1) mod N; count = count - 1; return val.
Double-ended queue (Deque): a linear collection that allows insertion and deletion at both ends. Operations:
- push_front(x), push_back(x)
- pop_front(), pop_back()
- front()/back(), isEmpty(), isFull() (if bounded) Implementation with circular array is similar: keep a front index and count, then compute back index as (front + count - 1) mod N. Variants commonly asked in exams: input-restricted deque (insertion only at one end, deletion at both) and output-restricted deque (insertion at both ends, deletion only at one end).
Linked-list implementations: both Queue and Deque can be implemented with singly or doubly linked lists. For a queue, keep pointers to head (front) and tail (rear) to support O(1) enqueue and dequeue. For a deque, a doubly linked list with head and tail pointers gives O(1) operations at both ends.
Complexities: For typical array or linked implementations:
- enqueue, dequeue, push_front, push_back, pop_front, pop_back: O(1) time.
- Memory: bounded array uses O(N), linked uses O(k) where k is number of elements plus pointer overhead.
Correctness conditions and common variants:
- Distinguish empty vs full: either store count, or reserve one unused slot and use condition (rear+1)%N == front as full, and front == rear as empty (under the reserved-slot convention where rear points to the next insertion position). State these choices in answers.
- When asked to show array after operations, list indices and front/rear values.
Formulas
rear = (rear + 1) % N — rear index advance by one in circular array; N is array size.
front = (front + 1) % N — front index advance by one in circular array.
back_index = (front + count - 1) % N — index of last element when using front+count representation.
isFull: count == N — using count to detect full for bounded queue.
isEmpty: count == 0 — using count to detect empty. Reserved-slot convention (rear is next insertion position): isEmpty: front == rear, isFull: (rear + 1) % N == front.
Complexity notes (syntax to remember): enqueue: O(1), dequeue: O(1), memory: O(N) for array.
(Each symbol: N is capacity, count is number of elements — no SI units apply. Assume N > 0.)
Worked examples
- Manual circular queue trace (exam style) Problem: start with empty circular queue of capacity N = 5. Perform: enqueue 10, enqueue 20, enqueue 30, dequeue, enqueue 40, enqueue 50, enqueue 60, then show queue contents, front, rear, count. Solution (step-by-step):
- Start: front = 0, rear = -1, count = 0, array indexes 0..4 empty.
- enqueue 10: rear = ( -1 + 1 ) % 5 = 0, arr[0] = 10, count = 1.
- enqueue 20: rear = 1, arr[1] = 20, count = 2.
- enqueue 30: rear = 2, arr[2] = 30, count = 3.
- dequeue: val = arr[front] = arr[0] = 10, front = (0 + 1) % 5 = 1, count = 2.
- enqueue 40: rear = 3, arr[3] = 40, count = 3.
- enqueue 50: rear = 4, arr[4] = 50, count = 4.
- enqueue 60: rear = (4 + 1) % 5 = 0, arr[0] = 60, count = 5 (queue now full). Final: front = 1, rear = 0, count = 5. Contents from front: indices 1,2,3,4,0 => 20 30 40 50 60.
- C++ program: circular queue (array, commands) Complete program below reads capacity and a sequence of operations. It prints the final queue when command 3 is given.
#include <bits/stdc++.h>
using namespace std;
int main(){
int N; if(!(cin>>N)) return 0;
int m; cin>>m;
vector<int> arr(N);
int front = 0, rear = -1, count = 0;
for(int i=0;i<m;i++){
int cmd; cin>>cmd;
if(cmd==1){ int x; cin>>x; if(count==N) cout<<"Overflow\n"; else { rear=(rear+1)%N; arr[rear]=x; count++; }}
else if(cmd==2){ if(count==0) cout<<"Underflow\n"; else { cout<<arr[front]<<"\n"; front=(front+1)%N; count--; }}
else if(cmd==3){ if(count==0) cout<<"Queue is empty\n"; else { cout<<"Queue contents:";
for(int k=0;k<count;k++) cout<<" "<<arr[(front+k)%N]; cout<<"\n"; }}
}
return 0;
}
Sample input:
5 8
1 10
1 20
1 30
2
1 40
1 50
1 60
3
Exact output (each line as shown):
10
Queue contents: 20 30 40 50 60
Explanation: the single printed line 10 comes from the dequeue command (cmd 2), then final print shows the queue contents.
- C++ program: bounded deque (array implementation) demonstration
#include <bits/stdc++.h>
using namespace std;
int main(){
int N; if(!(cin>>N)) return 0; int m; cin>>m;
vector<int> arr(N);
int front = 0, count = 0;
for(int i=0;i<m;i++){
int cmd; cin>>cmd;
if(cmd==1){ int x; cin>>x; // push_back
if(count==N) cout<<"Overflow\n";
else { arr[(front+count)%N]=x; count++; }
} else if(cmd==2){ int x; cin>>x; // push_front
if(count==N) cout<<"Overflow\n";
else { front = (front-1+N)%N; arr[front]=x; count++; }
} else if(cmd==3){ // pop_front
if(count==0) cout<<"Underflow\n";
else { cout<<arr[front]<<"\n"; front=(front+1)%N; count--; }
} else if(cmd==4){ // pop_back
if(count==0) cout<<"Underflow\n";
else { int idx=(front+count-1)%N; cout<<arr[idx]<<"\n"; count--; }
} else if(cmd==5){ // print
if(count==0) cout<<"Deque is empty\n";
else { cout<<"Deque contents:"; for(int k=0;k<count;k++) cout<<" "<<arr[(front+k)%N]; cout<<"\n"; }
}
}
return 0;
}
Sample input:
5 7
1 10
1 20
2 5
1 30
3
4
5
Explanation of commands: 1 x = push_back(x); 2 x = push_front(x); 3 = pop_front; 4 = pop_back; 5 = print.
Exact output:
5
30
Deque contents: 10 20
(First printed line 5 is the result of pop_front which printed the value 5 pushed earlier at front; next 30 from pop_back; final line shows remaining contents.)
Common mistakes
- Not updating count or front/rear correctly after enqueue/dequeue leading to stale indices.
- For deque, implementing pop_back wrong index (off-by-one): compute (front + count - 1) % N.
- Drawing diagrams without labeling the index numbers and front/rear positions — exams require those labels.
- Confusing FIFO property of queue with LIFO property of stack.
For your exam
Kinds of questions you will get:
- Definitions: define Queue, Deque, circular queue and state operations.
- Short notes: compare array vs linked implementations, mention time/memory complexity.
- Derivations / proof-style: show why circular indexing works; prove O(1) for enqueue/dequeue.
- Diagrams: draw array boxes 0..N-1 and show states after given operations (label front, rear, count).
- Programs: implement circular queue or deque (array or linked) in C/C++ and show sample output.
- Tracing problems: given sequence of operations, show array contents and front/rear values.
What a full-marks answer must include:
- Clear definition (bold the ADT and mention FIFO for queue).
- The chosen implementation convention (use of count or reserved slot) stated explicitly.
- Correct step-by-step algorithm pseudocode or code with complexity cost.
- For diagrams: indices labeled, elements placed, front/rear arrows and final list order.
- For programs: compile-able code and sample I/O.
Quick check
- In a circular array of size N, if front = 3 and count = 4, what is the index of the rear (last element)?
- Using the "count" convention, what conditions detect empty and full?
- For a deque implemented with a circular array, how do you compute the index used by push_front(x)?
Answers: 1) (front + count - 1) % N. 2) empty when count == 0, full when count == N. 3) front = (front - 1 + N) % N; store at arr[front].
Note to reviewer: I tried to be precise about the two common conventions (count vs one-slot). See verify list for specific claims to check.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?