Guest User

Untitled

a guest
May 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. import Foundation
  2.  
  3. public struct Stack<Element>
  4. {
  5. private var storage: [Element]
  6.  
  7. public init()
  8. {
  9. self.storage = [Element]()
  10. }
  11.  
  12. public mutating func push(_ element: Element) -> Void
  13. {
  14. self.storage.insert(element, at: 0)
  15. }
  16.  
  17. public mutating func pop() -> Element?
  18. {
  19. return self.storage.removeFirst()
  20. }
  21.  
  22. public func whosOnTop() -> Element?
  23. {
  24. return self.storage.first
  25. }
  26. }
  27.  
  28. //
  29. // TEST
  30. //
  31.  
  32. var stack = Stack<Int>()
  33.  
  34. stack.push(1)
  35. stack.push(2)
  36. stack.push(3)
  37. stack.push(4)
  38. stack.push(5)
  39.  
  40. if let top = stack.whosOnTop()
  41. {
  42. print("Top \(top)")
  43. }
  44.  
  45. print(stack)
  46.  
  47. if let fuera = stack.pop()
  48. {
  49. print("Fuera: \(fuera)")
  50. }
  51.  
  52. print(stack)
  53.  
  54. stack.push(5)
  55. print(stack)
Add Comment
Please, Sign In to add comment