-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
added implementing stack using two queues #10076
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c31c78
added implementing stack using two queues
anamithrak15 3c1d805
Update Stack using two queues
anamithrak15 c86b303
Update stack_using_two_queues.py
anamithrak15 dd84d40
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 36960c0
Update stack_using_two_queues.py
anamithrak15 fc4868b
Merge branch 'master' of https://github.com/anamithrak15/Python
anamithrak15 93a0846
Update stack_using_two_queues.py
cclauss 76e6a93
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7d1ee9b
Update stack_using_two_queues.py
cclauss c05bf61
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9005ef7
Update stack_using_two_queues.py
cclauss 063b283
Update stack_using_two_queues.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from collections import deque | ||
from dataclasses import dataclass, field | ||
|
||
|
||
@dataclass | ||
class StackWithQueues: | ||
""" | ||
https://www.geeksforgeeks.org/implement-stack-using-queue/ | ||
|
||
>>> stack = StackWithQueues() | ||
>>> stack.push(1) | ||
>>> stack.push(2) | ||
>>> stack.push(3) | ||
>>> stack.peek() | ||
3 | ||
>>> stack.pop() | ||
3 | ||
>>> stack.peek() | ||
2 | ||
>>> stack.pop() | ||
2 | ||
>>> stack.pop() | ||
1 | ||
>>> stack.peek() is None | ||
True | ||
>>> stack.pop() | ||
Traceback (most recent call last): | ||
... | ||
IndexError: pop from an empty deque | ||
""" | ||
main_queue: deque[int] = field(default_factory=deque) | ||
temp_queue: deque[int] = field(default_factory=deque) | ||
|
||
def push(self, item): | ||
self.temp_queue.append(item) | ||
while self.main_queue: | ||
self.temp_queue.append(self.main_queue.popleft()) | ||
self.main_queue, self.temp_queue = self.temp_queue, self.main_queue | ||
|
||
def pop(self): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self.main_queue.popleft() | ||
|
||
def peek(self): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self.main_queue[0] if self.main_queue else None | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
stack = StackWithQueues() | ||
while stack: | ||
print("\nChoose operation:") | ||
print("1. Push") | ||
print("2. Pop") | ||
print("3. Peek") | ||
print("4. Quit") | ||
|
||
choice = input("Enter choice (1/2/3/4): ") | ||
|
||
if choice == "1": | ||
element = input("Enter element to push: ") | ||
stack.push(element) | ||
print(f"{element} pushed onto the stack.") | ||
elif choice == "2": | ||
popped_element = stack.pop() | ||
if popped_element is not None: | ||
print(f"Popped element: {popped_element}") | ||
else: | ||
print("Stack is empty.") | ||
elif choice == "3": | ||
peeked_element = stack.peek() | ||
if peeked_element is not None: | ||
print(f"Top element: {peeked_element}") | ||
else: | ||
print("Stack is empty.") | ||
elif choice == "4": | ||
del stack | ||
stack = None | ||
else: | ||
print("Invalid choice. Please try again.") |
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.
Uh oh!
There was an error while loading. Please reload this page.