Added Circular Queue using Two Stacks #1846
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Solved Issue No. #1793
Username: DinkyRajpoot56
Aim: Adding Circular Queue Using Two Stacks
Date: 10/11/2024
Issue No: #1793
Explanation:
Two Stacks (stack1 and stack2): stack1 is used for enqueue operations, while stack2 is used for dequeue operations.
Enqueue Operation: Pushes the element onto stack1.
Dequeue Operation: If stack2 is empty, all elements from stack1 are transferred to stack2, reversing their order to maintain the FIFO property. The top element of stack2 is then popped to simulate dequeue.
Display Operation: Prints elements in stack2 from top to bottom (front of the queue) followed by stack1 from bottom to top (end of the queue).
Complexity:
Time Complexity:
Enqueue:
𝑂
(
1
)
O(1)
Dequeue:
𝑂
(
𝑛
)
O(n) in the worst case (when transferring elements between stacks)
Space Complexity:
𝑂
(
𝑛
)
O(n) where
𝑛
n is the total number of elements in the queue.
This approach ensures that the queue behaves like a circular queue when combined with proper logic for handling full and empty cases.