-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
[FIX:]#10002 #10109
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
Closed
IndranjanaChatterjee
wants to merge
3
commits into
TheAlgorithms:master
from
IndranjanaChatterjee:stack_made_with_two_queues
Closed
[FIX:]#10002 #10109
Changes from all commits
Commits
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 was deleted.
Oops, something went wrong.
This file contains 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,70 @@ | ||
from collections import deque | ||
from typing import Generic, TypeVar | ||
|
||
T = TypeVar("T") | ||
""" Implementing stack using two arrays""" | ||
|
||
|
||
class Stack(Generic[T]): # Stack class to implement stack operations | ||
def __init__(self): | ||
self.insert_queue = deque() # First Queue to be used for inserting | ||
self.second_queue = deque() # Second Queue to be used | ||
|
||
def push(self, item: int): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
self.insert_queue.append(item) | ||
while self.second_queue: | ||
self.insert_queue.append( | ||
self.second_queue.popleft() | ||
) # Popping the elements | ||
|
||
self.insert_queue, self.second_queue = self.second_queue, self.insert_queue | ||
|
||
def pop(self) -> int: | ||
if not self.second_queue: # if the stack is empty | ||
return None | ||
return self.second_queue.popleft() # if not empty pop | ||
|
||
def top(self) -> int: | ||
if not self.second_queue: | ||
return None | ||
return self.second_queue[0] | ||
|
||
def __str__(self) -> str: | ||
return tuple(self.second_queue) | ||
|
||
def __len__(self) -> int: | ||
return len(self.second_queue) | ||
|
||
|
||
def test_stack() -> None: | ||
stack = Stack() # Creating a empty stack | ||
choice = int( | ||
input("1 to push,2 to pop,3 to peek ,4 to print, 5 for size,6 to exit:") | ||
) | ||
while choice in (1, 2, 3, 4, 5, 6): | ||
match (choice): | ||
case 1: | ||
element = int(input("Enter the element to push:")) | ||
stack.push(element) | ||
case 2: | ||
print(stack.pop()) | ||
case 3: | ||
print(stack.top()) | ||
case 4: | ||
print(stack.__str__()) | ||
case 5: | ||
leng = stack.__len__() | ||
print(f"The size of the stack is {leng}") | ||
case 6: | ||
print("Exiting") | ||
break | ||
case _: | ||
print("Enter properly") | ||
|
||
choice = int( | ||
input("1 to push,2 to pop,3 to peek ,4 to print, 5 for size,6 to exit:") | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_stack() # calling the test function |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
__init__
. If the function does not return a value, please provide the type hint as:def function() -> None: