Input:
Operations: ["enqueue(1)","enqueue(2)","front()","dequeue()","isEmpty()"]Output: [null,null,1,1,false]Explanation:
1. Enqueue 1: The queue becomes [1].2. Enqueue 2: The queue becomes [1,2].3. Front: The front element is1.4. Dequeue: Remove the front element 1. The queue becomes [2].5. isEmpty: The queue is not empty, hence returns false.
A queue data structure allows elements to be added at the rear and removed from the front. An array can be effectively used to implement a fixed-size queue by maintaining two pointers: one for the front and one for the rear. Additionally, we maintain a variable to keep track of the number of elements in the queue.
Enqueue: Add an element to the queue after ensuring it is not full. If rear < n, indicating that the array has space, store the element at arr[rear] and increment rear by 1. If rear == n, it is an Overflow condition as the array is full.
Dequeue: Remove an element from the queue if there is at least one element to delete, i.e., rear > 0. Delete the element at arr[front], and shift all remaining elements to the left by one position to allow for the dequeuing of the next element at the front.
Front: Retrieve the front element from the queue, i.e., arr[front], if the queue is not empty.
Display: Print all elements of the queue. Traverse and print elements from index front to rear if the queue is not empty.
publicclassFixedArrayQueueBadRemove {
privateint[] arr; // array to store queue elementsprivateint front; // front points to the front element in the queueprivateint rear; // rear points to the last element in the queueprivateint capacity; // maximum capacity of the queueprivateint count; // current size of the queue// Constructor to initialize a queuepublicFixedArrayQueueBadRemove(int size) {
arr =newint[size];
capacity = size;
front = 0;
rear = 0;
count = 0;
}
// Insert an element at the rearpublicbooleanenqueue(int item) {
if (isFull()) {
thrownew IllegalStateException("Queue is full");
}
arr[rear++]= item;
count++;
returntrue;
}
// Remove and return the front elementpublicintdequeue() {
if (isEmpty()) {
thrownew IllegalStateException("Queue is empty");
}
int temp = arr[front];
for (int i = 0; i < rear - 1; i++) {
arr[i]= arr[i + 1];
}
arr[--rear]= 0;
count--;
return temp;
}
// Return the front element without removing itpublicintpeek() {
if (isEmpty()) {
thrownew IllegalStateException("Queue is empty");
}
return arr[front];
}
// Return the size of the queuepublicintsize() {
return count;
}
// Check if the queue is emptypublicbooleanisEmpty() {
return count == 0;
}
// Check if the queue is fullpublicbooleanisFull() {
return rear == capacity;
}
}
classFixedArrayQueueBadRemove:
def __init__(self, size: int):
self.arr = [0] * size # array to store queue elements self.front =0# front points to the front element in the queue self.rear =0# rear points to the last element in the queue self.capacity = size # maximum capacity of the queue self.count =0# current size of the queue# Insert an element at the reardefenqueue(self, item: int) -> bool:
if self.is_full():
raiseOverflowError("Queue is full")
self.arr[self.rear] = item
self.rear +=1 self.count +=1returnTrue# Remove and return the front elementdefdequeue(self) -> int:
if self.is_empty():
raiseIndexError("Queue is empty")
temp = self.arr[self.front]
for i in range(self.rear -1):
self.arr[i] = self.arr[i +1]
self.rear -=1 self.count -=1 self.arr[self.rear] =0return temp
# Return the front element without removing itdefpeek(self) -> int:
if self.is_empty():
raiseIndexError("Queue is empty")
return self.arr[self.front]
# Return the size of the queuedefsize(self) -> int:
return self.count
# Check if the queue is emptydefis_empty(self) -> bool:
return self.count ==0# Check if the queue is fulldefis_full(self) -> bool:
return self.rear == self.capacity
⏰ Time complexity: O(1). The complexity for all operations (enqueue, dequeue, isEmpty, isFull, front, and size) is O(1) since we are using direct indexing and simple arithmetic operations.