Advertisement
Sunilsai

Queue_Implementation_Stack_Type1

May 27th, 2022
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. """
  2. S1 -> S2
  3. X -> S1
  4. S2 -> S1
  5. """
  6. class Queue:
  7.     def __init__(self):
  8.         self.OneStack = []
  9.         self.TwoStack = []
  10.    
  11.     def Push(self, Val):
  12.         while(self.OneStack):
  13.             self.TwoStack.append(self.OneStack.pop())
  14.         self.OneStack.append(Val)
  15.         while(self.TwoStack):
  16.             self.OneStack.append(self.TwoStack.pop())
  17.        
  18.     def Pop(self):
  19.         print(self.OneStack.pop())
  20.    
  21.     def Top(self):
  22.         print(self.OneStack[-1])
  23.    
  24. ObjQueue = Queue()
  25. ObjQueue.Push(1)
  26. ObjQueue.Push(2)
  27. ObjQueue.Push(4)
  28. ObjQueue.Pop()
  29. ObjQueue.Top()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement