Advertisement
Sunilsai

Stack_Implementation_Using_Single_Queue

May 25th, 2022
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. class MyStack:
  2.     def __init__(self):
  3.         self.OneQueue = []
  4.  
  5.     def push(self, x: int) -> None:
  6.         self.OneQueue.append(x)
  7.         for i in range(len(self.OneQueue)-1):
  8.             self.OneQueue.append(self.OneQueue.pop(0))
  9.  
  10.     def pop(self) -> int:
  11.         return self.OneQueue.pop(0)
  12.  
  13.     def top(self) -> int:
  14.         return self.OneQueue[0]
  15.  
  16.     def empty(self) -> bool:
  17.         return self.OneQueue == []
  18.  
  19.  
  20. # Your MyStack object will be instantiated and called as such:
  21. # obj = MyStack()
  22. # obj.push(x)
  23. # param_2 = obj.pop()
  24. # param_3 = obj.top()
  25. # param_4 = obj.empty()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement