Problem Link - isEmpty,isFull, functions of Queue
Problem Statement:
We are implementing queue using an array, We first initialize an array with a fixed size which will be the max size of our queue. we will be also using three global variable here :-
- front - for tracing the index of first element of queue.
- rear - for tracing the index of rear element of queue.
- maxSize - storing the max size of array.
Approach:
The key idea is to manage the front and rear pointers of the queue while maintaining the current size. Here’s a detailed breakdown of the logic:
-
Checking if the Queue is Empty:
- The
isEmpty
function checks if thecurrentSize
is 0. If it is, the queue is empty, and the function returns true; otherwise, it returns false.
- The
-
Checking if the Queue is Full:
- The
isFull
function checks if thecurrentSize
equalsmaxSize
. If it does, it means the queue has reached its maximum capacity, and the functionreturns true
otherwise, itreturns false
.
- The
-
Getting the Size of the Queue:
- The
size
function simply returns the maximum size of the queue (maxSize
). This could be modified to return the current number of elements in the queue instead, but in this snippet, it shows the fixed capacity.
- The
Time Complexity:
- The time complexity of the operations
isEmpty
,isFull
, andsize
is O(1), as they involve simple checks and return statements.
Space Complexity:
- The space complexity is O(maxSize) since we are using a fixed-size array to store the elements of the queue.