Advertisement
Sunilsai

Queue_Implementation_Stack_Type2

May 27th, 2022
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. """
  2. -> Direct Push In Stack One
  3. -> When Called Pop If Stack Two Is Empty Load All Stack One Elements In Stack Two Else
  4.    Pop From Stack Two
  5. """
  6. class Queue:
  7.     def __init__(self):
  8.         self.OneStack = []
  9.         self.TwoStack = []
  10.    
  11.     def Push(self, Val):
  12.         self.OneStack.append(Val)
  13.        
  14.     def Pop(self):
  15.         if(self.TwoStack):
  16.             print(self.TwoStack.pop())
  17.         else:
  18.             while(self.OneStack):
  19.                 self.TwoStack.append(self.OneStack.pop())
  20.             print(self.TwoStack.pop())
  21.    
  22.     def Top(self):
  23.         print(self.TwoStack[-1])
  24.    
  25. ObjQueue = Queue()
  26. ObjQueue.Push(1)
  27. ObjQueue.Push(2)
  28. ObjQueue.Push(4)
  29. ObjQueue.Pop()
  30. ObjQueue.Pop()
  31. ObjQueue.Pop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement