-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
stack_using_two_queues #10002
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
stack_using_two_queues #10002
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||||||||||||||||||||
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) -> None: | ||||||||||||||||||||||
self.insert_queue = deque() # First Queue to be used for inserting | ||||||||||||||||||||||
self.suffle_queue = deque() # Second Queue to be used for suffling | ||||||||||||||||||||||
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.
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. OPTIONAL: Should |
||||||||||||||||||||||
|
||||||||||||||||||||||
def push(self, item: int) -> None: | ||||||||||||||||||||||
self.insert_queue.append(item) # Add items into the Queue | ||||||||||||||||||||||
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. The code already says what the comment repeats.
Suggested change
|
||||||||||||||||||||||
while self.suffle_queue: | ||||||||||||||||||||||
self.insert_queue.append( | ||||||||||||||||||||||
self.suffle_queue.popleft() | ||||||||||||||||||||||
) # Popping the elements | ||||||||||||||||||||||
Comment on lines
+16
to
+18
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. Let's not wrap lines just to make room for a comment.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
self.insert_queue, self.suffle_queue = self.suffle_queue, self.insert_queue | ||||||||||||||||||||||
|
||||||||||||||||||||||
def pop(self) -> int: | ||||||||||||||||||||||
if not (self.suffle_queue): # if the stack is empty | ||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||
return None | ||||||||||||||||||||||
return self.suffle_queue.popleft() # if not empty pop | ||||||||||||||||||||||
|
||||||||||||||||||||||
def top(self) -> int: | ||||||||||||||||||||||
if not (self.suffle_queue): | ||||||||||||||||||||||
return None | ||||||||||||||||||||||
return self.suffle_queue[0] | ||||||||||||||||||||||
|
||||||||||||||||||||||
def printing(self) -> None: | ||||||||||||||||||||||
print(self.suffle_queue) | ||||||||||||||||||||||
|
||||||||||||||||||||||
def size(self) -> int: | ||||||||||||||||||||||
return len(self.suffle_queue) | ||||||||||||||||||||||
Comment on lines
+32
to
+36
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. These
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def test_stack() -> None: | ||||||||||||||||||||||
s = Stack() # Creating a stack in S | ||||||||||||||||||||||
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. Single-letter variable names makes code look like it was written in the 1970's.
Suggested change
|
||||||||||||||||||||||
n = int(input("1 to push,2 to pop,3 to peek ,4 to print, 5 for size,6 to exit:")) | ||||||||||||||||||||||
while n in (1, 2, 3, 4, 5, 6): | ||||||||||||||||||||||
match (n): | ||||||||||||||||||||||
case 1: | ||||||||||||||||||||||
element = int(input("Enter the element to push:")) | ||||||||||||||||||||||
s.push(element) | ||||||||||||||||||||||
case 2: | ||||||||||||||||||||||
print(s.pop()) | ||||||||||||||||||||||
case 3: | ||||||||||||||||||||||
print(s.top()) | ||||||||||||||||||||||
case 4: | ||||||||||||||||||||||
s.printing() | ||||||||||||||||||||||
case 5: | ||||||||||||||||||||||
leng = s.size() | ||||||||||||||||||||||
print(f"The size of the stack is {leng}") | ||||||||||||||||||||||
case 6: | ||||||||||||||||||||||
print("Exiting") | ||||||||||||||||||||||
break | ||||||||||||||||||||||
case _: | ||||||||||||||||||||||
print("Enter properly") | ||||||||||||||||||||||
|
||||||||||||||||||||||
n = 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 funtion |
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.