Guest User

Untitled

a guest
Dec 13th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. class StackUsingTwoQueues {
  2. var mainQueue = [Int]()
  3. var tempQueue = [Int]()
  4.  
  5. func push(item: Int) {
  6. mainQueue.append(item)
  7. }
  8.  
  9. func pop() -> Int {
  10. if mainQueue.isEmpty {
  11. return -1
  12. }
  13.  
  14. while mainQueue.count > 1 {
  15. tempQueue.append(mainQueue.removeFirst())
  16. }
  17.  
  18. let popped = mainQueue.removeFirst()
  19. mainQueue = tempQueue
  20. tempQueue = []
  21. return popped
  22. }
  23. }
  24.  
  25. let stack = StackUsingTwoQueues()
  26. stack.push(item: 5)
  27. stack.push(item: 6)
  28. stack.push(item: 7)
Add Comment
Please, Sign In to add comment