📖 Scenario: You are building a simple queue system for a small store. Customers join the queue to wait for their turn. You will use an array to keep track of the queue.
🎯 Goal: Create a queue using an array with basic operations: enqueue (add), dequeue (remove), and display the queue.
📋 What You'll Learn
Create an array-based queue with fixed size 5
Implement enqueue to add elements to the queue
Implement dequeue to remove elements from the queue
Implement a function to display the current queue elements
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage waiting lines, like customers at a store or tasks waiting to be processed.
💼 Career
Understanding queue implementation helps in software development roles where task scheduling, buffering, or resource management is needed.
Progress0 / 4 steps
1
Create the queue array and variables
Create an integer array called queue of size 5. Also create two integer variables called front and rear and set both to -1.
DSA C++
Hint
Use int queue[5]; to create the array. Initialize front and rear to -1 to show the queue is empty.
2
Add the enqueue function
Write a function called enqueue that takes an integer value and adds it to the queue array. Update rear accordingly. If the queue is full (rear is 4), print "Queue is full" and do not add the value.
DSA C++
Hint
Check if rear is 4 to detect full queue. Increase rear and add value to queue[rear]. Set front to 0 if it was -1.
3
Add the dequeue function
Write a function called dequeue that removes and returns the front element from the queue. If the queue is empty (front is -1 or front > rear), print "Queue is empty" and return -1. Otherwise, return the element at queue[front] and increase front by 1.
DSA C++
Hint
Check if queue is empty by testing front. Return the front element and move front forward.
4
Display the queue and test operations
Write a function called displayQueue that prints all elements from front to rear separated by spaces. Then, in main, enqueue 10, 20, 30; dequeue one element; enqueue 40; and finally call displayQueue.
DSA C++
Hint
Loop from front to rear to print queue elements. In main, follow the exact enqueue and dequeue calls before displaying.
Practice
(1/5)
1. What is the main purpose of using an array to implement a queue?
easy
A. To store elements in a fixed-size linear order for FIFO operations
B. To store elements randomly for quick access
C. To sort elements automatically
D. To store elements in a LIFO order
Solution
Step 1: Understand queue behavior
A queue follows FIFO (First In First Out), meaning elements are processed in the order they arrive.
Step 2: Role of array in queue
An array stores elements in a fixed linear order, which helps maintain the FIFO order by using front and rear indices.
Final Answer:
To store elements in a fixed-size linear order for FIFO operations -> Option A
Quick Check:
Queue uses array for FIFO storage = C [OK]
Hint: Queue means FIFO; array stores elements linearly [OK]
Common Mistakes:
Confusing queue with stack (LIFO)
Thinking array sorts elements automatically
Assuming random access is main goal
2. Which of the following is the correct way to declare a queue array of size 5 in C++?
easy
A. array queue(5);
B. int queue[5];
C. int[5] queue;
D. queue int[5];
Solution
Step 1: Recall C++ array declaration syntax
In C++, arrays are declared as: type name[size]; so int queue[5]; is correct.
Step 2: Check other options
Options A, B, and C do not follow valid C++ syntax for array declaration.
Final Answer:
int queue[5]; -> Option B
Quick Check:
C++ array syntax = type name[size] [OK]
Hint: Remember C++ arrays: type name[size]; [OK]
Common Mistakes:
Placing size before name
Using wrong keywords like 'array'
Incorrect order of type and name
3. What will be the output after performing these operations on an empty queue of size 3 implemented using array? enqueue(10); enqueue(20); dequeue(); enqueue(30); display(); Assume enqueue adds at rear and dequeue removes from front.
medium
A. 10 -> 30 -> null
B. 10 -> 20 -> 30 -> null
C. 30 -> null
D. 20 -> 30 -> null
Solution
Step 1: Perform enqueue(10) and enqueue(20)
Queue becomes: 10 -> 20 -> null
Step 2: Perform dequeue()
Removes 10 from front, queue becomes: 20 -> null
Step 3: Perform enqueue(30)
Add 30 at rear, queue becomes: 20 -> 30 -> null
Step 4: Display queue
Output is: 20 -> 30 -> null
Final Answer:
20 -> 30 -> null -> Option D
Quick Check:
After dequeue and enqueue, queue = 20, 30 [OK]
Hint: Track front and rear after each operation [OK]
Common Mistakes:
Not removing element on dequeue
Adding new element at front instead of rear
Confusing order of elements
4. Identify the error in this queue implementation snippet using array:
int queue[3], front = -1, rear = -1;
void enqueue(int x) {
if (rear == 3) {
cout << "Queue Full";
return;
}
rear++;
queue[rear] = x;
if (front == -1) front = 0;
}
medium
A. front should be incremented in enqueue
B. rear should be initialized to 0
C. Condition should be rear == 2 to check full queue
D. No error, code is correct
Solution
Step 1: Understand array indexing for size 3
Array indices are 0, 1, 2. So rear should not exceed 2.
Step 2: Check condition for full queue
Condition rear == 3 is wrong because index 3 is out of bounds. It should be rear == 2.
Final Answer:
Condition should be rear == 2 to check full queue -> Option C
Quick Check:
Array max index = size-1 = 2 [OK]
Hint: Array max index = size - 1, check boundary carefully [OK]
Common Mistakes:
Using size instead of size-1 for index check
Incrementing front in enqueue
Initializing rear incorrectly
5. Given a queue implemented using an array of size 4, initially empty, what will be the state of the queue after these operations? enqueue(5); enqueue(10); dequeue(); enqueue(15); enqueue(20); dequeue(); enqueue(25); display(); Assume no circular queue and enqueue fails if rear reaches size-1.