Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Circular Queue using Two Stacks #1846

Merged
merged 1 commit into from
Nov 10, 2024

Conversation

DinkyRajpoot56
Copy link

@DinkyRajpoot56 DinkyRajpoot56 commented Nov 10, 2024

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.
Screenshot (3651)

Screenshot (3652)
Screenshot (3653)
Screenshot (3654)

@pankaj-bind pankaj-bind merged commit a9b0c8b into AlgoGenesis:main Nov 10, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants