Guest User

Untitled

a guest
Feb 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. struct Stack<T> {
  2. private var stack: [T]
  3.  
  4. init()
  5. {
  6. stack = [T]()
  7. }
  8.  
  9. mutating func push(val: T)
  10. {
  11. stack.append(val)
  12. }
  13.  
  14. mutating func pop() -> T
  15. {
  16. return stack.removeLast()
  17. }
  18.  
  19. var isEmpty: Bool {
  20. return stack.isEmpty
  21. }
  22.  
  23. var count : Int {
  24. return stack.count
  25. }
  26. }
  27.  
  28. // Int icin
  29. var stackInt = Stack<Int>()
  30.  
  31. stackInt.push(val: 10)
  32. stackInt.push(val: 20)
  33. stackInt.push(val: 30)
  34.  
  35. while !stackInt.isEmpty {
  36. print(stackInt.pop())
  37. }
  38.  
  39. // String Icin
  40. var stackString = Stack<String>()
  41.  
  42. stackString.push(val: "Kerem")
  43. stackString.push(val: "Swift")
  44. stackString.push(val: "Python")
  45.  
  46. while !stackString.isEmpty {
  47. print(stackString.pop())
  48. }
Add Comment
Please, Sign In to add comment