From dc1580997c7fa24acd34b209380901244429d24f Mon Sep 17 00:00:00 2001 From: Indranjana Chatterjee Date: Sat, 7 Oct 2023 12:31:08 +0530 Subject: [PATCH 1/2] stack_using_two_queues --- data_structures/stacks/stack_by_two_queues.py | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 data_structures/stacks/stack_by_two_queues.py diff --git a/data_structures/stacks/stack_by_two_queues.py b/data_structures/stacks/stack_by_two_queues.py new file mode 100644 index 000000000000..2955c0c45414 --- /dev/null +++ b/data_structures/stacks/stack_by_two_queues.py @@ -0,0 +1,78 @@ +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.suffle_Queue=deque()#Second Queue to be used for suffling + + + def push(self,item:int): + self.insert_Queue.append(item)#Add items into the Queue + while(self.suffle_Queue): + self.insert_Queue.append(self.suffle_Queue.popleft())#Poping the elements + + self.insert_Queue,self.suffle_Queue=self.suffle_Queue,self.insert_Queue + + + def pop(self): + if(not(self.suffle_Queue)):#if the stack is empty + return None + return(self.suffle_Queue.popleft())#if not empty pop + + + def top(self): + if(not(self.suffle_Queue)): + return None + return(self.suffle_Queue[0]) + + + def printing(self): + print(self.suffle_Queue) + + + def size(self): + return(len(self.suffle_Queue)) + + + +def test_stack(): + s = Stack()#Creating a stack in S + n=int(input("1 to push 2 to pop and 3 to peek 4 to print and 5 for size of the stack and 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 and 3 to peek 4 to print and 5 for of the stack and 6 to exit:")) + + +if __name__=="__main__": + test_stack()#calling the test funtion + + + + + From 6ffb09053824fb0d34df2cf888b175569a3482b3 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 07:03:49 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/stack_by_two_queues.py | 120 +++++++++--------- 1 file changed, 57 insertions(+), 63 deletions(-) diff --git a/data_structures/stacks/stack_by_two_queues.py b/data_structures/stacks/stack_by_two_queues.py index 2955c0c45414..02f2fbc33620 100644 --- a/data_structures/stacks/stack_by_two_queues.py +++ b/data_structures/stacks/stack_by_two_queues.py @@ -4,75 +4,69 @@ 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.suffle_Queue=deque()#Second Queue to be used for suffling - - - def push(self,item:int): - self.insert_Queue.append(item)#Add items into the Queue - while(self.suffle_Queue): - self.insert_Queue.append(self.suffle_Queue.popleft())#Poping the elements - - self.insert_Queue,self.suffle_Queue=self.suffle_Queue,self.insert_Queue - - + +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.suffle_Queue = deque() # Second Queue to be used for suffling + + def push(self, item: int): + self.insert_Queue.append(item) # Add items into the Queue + while self.suffle_Queue: + self.insert_Queue.append(self.suffle_Queue.popleft()) # Poping the elements + + self.insert_Queue, self.suffle_Queue = self.suffle_Queue, self.insert_Queue + def pop(self): - 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 - - + return self.suffle_Queue.popleft() # if not empty pop + def top(self): - if(not(self.suffle_Queue)): + if not (self.suffle_Queue): return None - return(self.suffle_Queue[0]) - - + return self.suffle_Queue[0] + def printing(self): print(self.suffle_Queue) - - + def size(self): - return(len(self.suffle_Queue)) - - + return len(self.suffle_Queue) + def test_stack(): - s = Stack()#Creating a stack in S - n=int(input("1 to push 2 to pop and 3 to peek 4 to print and 5 for size of the stack and 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 and 3 to peek 4 to print and 5 for of the stack and 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 and 3 to peek 4 to print and 5 for size of the stack and 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 and 3 to peek 4 to print and 5 for of the stack and 6 to exit:" + ) + ) + + +if __name__ == "__main__": + test_stack() # calling the test funtion