From 8c516b75d5c48acc75fc51621dd2b4be9b92ed10 Mon Sep 17 00:00:00 2001 From: Indranjana Chatterjee Date: Sat, 7 Oct 2023 15:04:50 +0530 Subject: [PATCH 1/2] stack_using_two_queues --- .../stacks/stack_with_two_queues.py | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 data_structures/stacks/stack_with_two_queues.py diff --git a/data_structures/stacks/stack_with_two_queues.py b/data_structures/stacks/stack_with_two_queues.py new file mode 100644 index 000000000000..052ab6a3985a --- /dev/null +++ b/data_structures/stacks/stack_with_two_queues.py @@ -0,0 +1,79 @@ +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 + + + def push(self,item:int) -> None: + self.insert_queue.append(item)#Add items into the Queue + while(self.suffle_queue): + self.insert_queue.append(self.suffle_queue.popleft())#Popping the elements + + 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 + 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)) + + + +def test_stack() -> None: + s = Stack()#Creating a stack in S + 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 + + + + + From 0db9928be404903b4d1a0ad39a11df154494380c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 09:37:48 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../stacks/stack_with_two_queues.py | 121 ++++++++---------- 1 file changed, 55 insertions(+), 66 deletions(-) diff --git a/data_structures/stacks/stack_with_two_queues.py b/data_structures/stacks/stack_with_two_queues.py index 052ab6a3985a..559f4192a41f 100644 --- a/data_structures/stacks/stack_with_two_queues.py +++ b/data_structures/stacks/stack_with_two_queues.py @@ -4,76 +4,65 @@ T = TypeVar("T") """ Implementing stack using two arrays""" -class Stack(Generic[T]):#Stack class to implement stack operations - - - - - + +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 - - - def push(self,item:int) -> None: - self.insert_queue.append(item)#Add items into the Queue - while(self.suffle_queue): - self.insert_queue.append(self.suffle_queue.popleft())#Popping the elements - - 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 + self.insert_queue = deque() # First Queue to be used for inserting + self.suffle_queue = deque() # Second Queue to be used for suffling + + def push(self, item: int) -> None: + self.insert_queue.append(item) # Add items into the Queue + while self.suffle_queue: + self.insert_queue.append( + self.suffle_queue.popleft() + ) # Popping the elements + + 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 return None - return(self.suffle_queue.popleft())#if not empty pop - - + return self.suffle_queue.popleft() # if not empty pop + def top(self) -> int: - if(not(self.suffle_queue)): + if not (self.suffle_queue): return None - return(self.suffle_queue[0]) - - - def printing(self) ->None: + return self.suffle_queue[0] + + def printing(self) -> None: print(self.suffle_queue) - - - def size(self) ->int: - return(len(self.suffle_queue)) - - + + def size(self) -> int: + return len(self.suffle_queue) + def test_stack() -> None: - s = Stack()#Creating a stack in S - 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 - - - - - + s = Stack() # Creating a stack in S + 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