
Problem
Solution
Submissions
Queue using Arrays
Certification: Basic Level
Accuracy: 0%
Submissions: 0
Points: 5
Write a C program to implement a queue data structure using arrays. The queue should support the following operations:
enqueue(int x)
: Add an item to the rear of the queuedequeue()
: Remove an item from the front of the queue and return itfront()
: Get the front item from the queue without removing itisEmpty()
: Check if the queue is emptyisFull()
: Check if the queue is full
Example 1
- Input: enqueue(10) enqueue(20) enqueue(30) front() dequeue() front() dequeue() dequeue() isEmpty()
- Output: 10, 10, 20, 30, true
- Explanation:
Step 1: enqueue(10) - Queue becomes [10]
Step 2: enqueue(20) - Queue becomes [10, 20]
Step 3: enqueue(30) - Queue becomes [10, 20, 30]
Step 4: front() returns 10 (the front element)
Step 5: dequeue() returns and removes 10 - Queue becomes [20, 30]
Step 6: front() returns 20 (the new front element)
Step 7: dequeue() returns and removes 20 - Queue becomes [30]
Step 8: dequeue() returns and removes 30 - Queue becomes []
Step 9: isEmpty() returns true as the queue is now empty
Example 2
- Input: isEmpty() enqueue(5) isEmpty() isFull() enqueue(10) enqueue(15) enqueue(20) enqueue(25) isFull() dequeue() dequeue() front()
- Output: true, false, false, true, 5, 10, 15
- Explanation:
Step 1: isEmpty() returns true as queue is initially empty
Step 2: enqueue(5) - Queue becomes [5]
Step 3: isEmpty() returns false as queue now has an element
Step 4: isFull() returns false as queue is not full yet
Step 5: enqueue(10) - Queue becomes [5, 10]
Step 6: enqueue(15) - Queue becomes [5, 10, 15]
Step 7: enqueue(20) - Queue becomes [5, 10, 15, 20]
Step 8: enqueue(25) - Queue becomes [5, 10, 15, 20, 25]
Step 9: isFull() returns true as queue has reached its capacity
Step 10: dequeue() returns and removes 5 - Queue becomes [10, 15, 20, 25]
Step 11: dequeue() returns and removes 10 - Queue becomes [15, 20, 25]
Step 12: front() returns 15 (the current front element)
Constraints
- The maximum size of the queue is fixed (e.g., 100)
- All operations should have a time complexity of O(1)
- The queue should handle overflow (when trying to enqueue to a full queue) and underflow (when trying to dequeue from an empty queue)
- You must implement a circular queue to efficiently use the array space
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use an array to store the queue elements
- Maintain two pointers: front and rear
- The front pointer points to the first element of the queue
- The rear pointer points to the position where the next element will be inserted
- When adding an element (enqueue), increment the rear pointer
- When removing an element (dequeue), increment the front pointer
- Use modulo arithmetic to implement circular behavior for efficient space utilization