Skip to content

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions data_structures/stacks/stack_with_two_queues.py
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
Comment on lines +5 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
""" Implementing stack using two arrays"""
class Stack(Generic[T]): # Stack class to implement stack operations
class Stack(Generic[T]):
""" Implement a stack using two queues"""

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suffling is not a word.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OPTIONAL: Should Stack be a dataclass?


def push(self, item: int) -> None:
self.insert_queue.append(item) # Add items into the Queue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code already says what the comment repeats.

Suggested change
self.insert_queue.append(item) # Add items into the Queue
self.insert_queue.append(item)

while self.suffle_queue:
self.insert_queue.append(
self.suffle_queue.popleft()
) # Popping the elements
Comment on lines +16 to +18
Copy link
Member

Choose a reason for hiding this comment

The 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.append(
self.suffle_queue.popleft()
) # Popping the elements
# Popping the elements
self.insert_queue.append(self.suffle_queue.popleft())


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not (self.suffle_queue): # if the stack is empty
if not self.suffle_queue: # if the stack is empty

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These magic methods will allow us to just print(stack) and len(stack).

Suggested change
def printing(self) -> None:
print(self.suffle_queue)
def size(self) -> int:
return len(self.suffle_queue)
def __str__(self) -> str:
return tuple(self.suffle_queue)
def __len__(self) -> int:
return len(self.suffle_queue)



def test_stack() -> None:
s = Stack() # Creating a stack in S
Copy link
Member

Choose a reason for hiding this comment

The 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
s = Stack() # Creating a stack in S
stack = Stack() # Create a empty stack

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