mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
ed1df8f539
commit
85ac78ed08
@ -4,36 +4,30 @@ 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):
|
||||
self.insert_queue.append(item)
|
||||
while(self.second_queue):
|
||||
self.insert_queue.append(self.second_queue.popleft()) # Popping the elements
|
||||
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
|
||||
|
||||
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])
|
||||
|
||||
return self.second_queue[0]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return tuple(self.second_queue)
|
||||
@ -42,11 +36,12 @@ class Stack(Generic[T]):#Stack class to implement stack operations
|
||||
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)):
|
||||
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:"))
|
||||
@ -66,13 +61,10 @@ def test_stack() -> None:
|
||||
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:"))
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user