Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.31 KB | None | 0 0
  1. import Foundation
  2.  
  3. class OneWayLinkedList: CustomStringConvertible {
  4.     class Node {
  5.         private(set) var next: Node?
  6.         private(set) var value: Int
  7.        
  8.         init(_ value: Int, next: Node?) {
  9.             self.value = value
  10.             self.next = next
  11.         }
  12.     }
  13.    
  14.     var description: String {
  15.         if self.head == nil { return "nil"}
  16.         var str = ""
  17.         var node = self.head
  18.         while let n = node {
  19.             str.append("\(n.value)")
  20.             node = node?.next
  21.             if let _ = node { str.append("->") }
  22.         }
  23.         return str
  24.     }
  25.    
  26.     private(set) var head: Node?
  27.    
  28.     init(nodesCount: Int) {
  29.         var nextNode: Node? = nil
  30.         (0..<nodesCount).forEach {
  31.             let node = Node(nodesCount - $0 - 1, next: nextNode)
  32.             nextNode = node
  33.         }
  34.         self.head = nextNode
  35.     }
  36. }
  37.  
  38. class TwoWayLinkedList: CustomStringConvertible {
  39.     class Node {
  40.         var next: Node?
  41.         var previous: Node?
  42.         private(set) var value: Int
  43.         init(_ value: Int) {
  44.             self.value = value
  45.         }
  46.     }
  47.    
  48.     var description: String {
  49.         if self.head == nil { return "nil"}
  50.         var str = ""
  51.         var node = self.head
  52.         while let n = node {
  53.             str.append("\(n.value)")
  54.             node = node?.next
  55.             if let _ = node {str.append("<->")}
  56.         }
  57.         return str
  58.     }
  59.     private(set) var head: Node?
  60.     private(set) var tail: Node?
  61.    
  62.     func unshift(_ value: Int) {
  63.         let node = Node(value)
  64.         if self.tail == nil { self.tail = node}
  65.         if let head = self.head {
  66.             node.next = head
  67.             head.previous = node
  68.         }
  69.         self.head = node
  70.     }
  71. }
  72.  
  73. func splitList(starting head: OneWayLinkedList.Node?) -> [TwoWayLinkedList] {
  74.     var node = head
  75.     let odd = TwoWayLinkedList()
  76.     let even = TwoWayLinkedList()
  77.     var isOdd = true
  78.     while let n = node {
  79.         isOdd ? odd.unshift(n.value) : even.unshift(n.value)
  80.         node = n.next
  81.         isOdd = !isOdd
  82.     }
  83.    
  84.     return [odd, even]
  85. }
  86.  
  87. //test
  88.  
  89. let oneWayList = OneWayLinkedList(nodesCount: 20)
  90. let lists = splitList(starting: oneWayList.head)
  91. print(oneWayList)
  92. print("odd:  \(lists[0])")
  93. print("even: \(lists[1])")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement