diff --git a/Queue.playground/Contents.swift b/Queue.playground/Contents.swift new file mode 100644 index 0000000..90beda2 --- /dev/null +++ b/Queue.playground/Contents.swift @@ -0,0 +1,24 @@ + +typealias CQueueInt = CQueue + +let testQ : CQueueInt = CQueueInt() + +testQ.Push_Back(value: 1) +testQ.Push_Back(value: 2) +testQ.Push_Back(value: 3) +testQ.Push_Back(value: 4) +testQ.Push_Back(value: 5) +testQ.Push_Back(value: 6) +testQ.Push_Back(value: 7) +testQ.Push_Back(value: 8) + +print(testQ.Count()) + +while testQ.Empty() == false { + let v = testQ.Front() + print(v ?? "v is nil") + let v2 = testQ.Pop_Front() + print(v2 ?? "v2 is nil") + print(testQ.Count()) + +} diff --git a/Queue.playground/Sources/CQueue.swift b/Queue.playground/Sources/CQueue.swift new file mode 100644 index 0000000..0672b25 --- /dev/null +++ b/Queue.playground/Sources/CQueue.swift @@ -0,0 +1,70 @@ +// +// CQueue.swift +// A queue class implemented by single linkedin list class CSingleList +// +// Created by Zhaohui Xing on 02/06/19. +// Copyright © 2019 Zhaohui Xing. All rights reserved. +// + +import Foundation + +public class CQueue { + let m_Container: CSingleLinkedList + + //Make default initializer public to be accessible + public init() { + m_Container = CSingleLinkedList() + } +} + +extension CQueue { + public func Push_Back(value: T) { + let newNode : CSingleLinkedListNode = CSingleLinkedListNode(data:value) + m_Container.Append(value: newNode) + } +} + +extension CQueue { + public func Pop_Front()->T? { + var tempValue : T? + tempValue = nil + + if m_Container.head != nil { + tempValue = m_Container.head?.data + m_Container.RemoveHead() + } + + return tempValue + } +} + +extension CQueue { + public func Front()->T? { + var tempValue : T? + tempValue = nil + + if m_Container.head != nil { + tempValue = m_Container.head?.data + } + + return tempValue + } +} + +extension CQueue { + public func Empty()->Bool { + var ret : Bool = true + + if m_Container.head != nil { + ret = false + } + return ret + } +} + +extension CQueue { + public func Count()->Int { + let ret : Int = m_Container.count + return ret + } +} diff --git a/Queue.playground/Sources/CSingleLList.swift b/Queue.playground/Sources/CSingleLList.swift new file mode 100644 index 0000000..49b6d69 --- /dev/null +++ b/Queue.playground/Sources/CSingleLList.swift @@ -0,0 +1,182 @@ +// +// CSingleLList.swift +// LinkList +// +// Created by Zhaohui Xing on 10/26/18. +// Copyright © 2018 Zhaohui Xing. All rights reserved. +// +import Foundation + +//class base sll as reference type +public class CSingleLinkedList { //Equatable for ! operator in line#60 + var m_Head: CSingleLinkedListNode? + var m_Tail: CSingleLinkedListNode? + + init() { + } + + public init(head: CSingleLinkedListNode) { + self.m_Head = head + self.m_Tail = self.m_Head?.tail + } +} + +extension CSingleLinkedList { + public var head : CSingleLinkedListNode? { + get { + return m_Head + } + set (newNode) { + m_Head = newNode; + m_Tail = newNode?.tail + } + } +} + +extension CSingleLinkedList { + public var tail : CSingleLinkedListNode? { + return m_Tail + } +} + +extension CSingleLinkedList { + public var count : Int { + var nCount : Int = 0 + var cur : CSingleLinkedListNode? = m_Head + + while cur != nil { + cur = cur?.next + nCount += 1 + } + + return nCount + } +} + +extension CSingleLinkedList { + public subscript (value : T) -> CSingleLinkedListNode? { + var cur : CSingleLinkedListNode? = m_Head + + while cur != nil && cur!.data != value { + cur = cur?.next + } + + return cur + } +} + +extension CSingleLinkedList { + public func reverse() { + let prev : CSingleLinkedListNode? = CSingleLinkedListNode(); //This avoid the tail node is nil and while loop cannot go through all nodes + prev?.next = m_Head //prev.next as temperory head shifting cursor + let cur : CSingleLinkedListNode? = m_Head //Using cur.next as moving cursor, Note: this approach the orginal head's next is altered after the loop + + while cur?.next != nil { + let temp: CSingleLinkedListNode? = cur?.next + cur?.next = temp?.next; + temp?.next = prev?.next + prev?.next = temp + } + m_Head = prev?.next + } +} + +extension CSingleLinkedList { + public func reverse2() { + let prev : CSingleLinkedListNode? = CSingleLinkedListNode(); //This avoid the tail node is nil and while loop cannot go through all nodes + prev?.next = m_Head //prev.next as temperory head shifting cursor + var cur : CSingleLinkedListNode? = m_Head?.next //Using cur as moving cursor + + while cur != nil { + let temp: CSingleLinkedListNode? = cur + cur = temp?.next; + temp?.next = prev?.next + prev?.next = temp + } + m_Head?.next = nil; //This one need this call, since using cur as moving cursor and orginal head.next is not altered + m_Head = prev?.next + } +} + +extension CSingleLinkedList { + public func iscycled()->(retFlag : Bool, cycleNode:CSingleLinkedListNode?, tailNode:CSingleLinkedListNode?) { + var bRet : (retFlag : Bool, cycleNode:CSingleLinkedListNode?, tailNode:CSingleLinkedListNode?) = (false, nil, nil) + + if m_Head != nil { + bRet = m_Head!.iscycled() + } + + return bRet + } +} + +extension CSingleLinkedList { + public func AddHead(head: CSingleLinkedListNode) { + head.next = m_Head; + m_Head = head; + } +} + +extension CSingleLinkedList { + public func RemoveHead() { + m_Head = m_Head?.next; + } +} + +extension CSingleLinkedList { + public func Append(value: CSingleLinkedListNode) { + if self.head == nil { + self.head = value + } + else { + m_Tail?.next = value + m_Tail = value + } + } +} + +extension CSingleLinkedList { + public func reverseBetween(start startIndex: Int, end endIndex: Int) { + if endIndex <= startIndex { + return + } + + var cur : CSingleLinkedListNode? = m_Head + + //tempPrev is the start node to swap sequence. + var tempPrev : CSingleLinkedListNode? //"?" allowing following calling "tempPrev = nil", otherwise compiling error for it + tempPrev = nil + + let steps : Int = (0 < startIndex) ? startIndex-1 : 0 + for _ in 0...steps { + if cur != nil { + tempPrev = cur + cur = cur?.next; + } + } + if cur == nil { + return + } + + var tempHead: CSingleLinkedListNode? = cur; + + + for _ in startIndex...(endIndex-1) { + if cur?.next != nil { + let temp: CSingleLinkedListNode? = cur?.next + cur?.next = temp?.next; + temp?.next = tempHead + tempHead = temp + } + } + + if tempPrev != nil { + tempPrev?.next = tempHead + } + else { + m_Head = tempHead + } + + } +} + diff --git a/Queue.playground/Sources/SingleLLNode.swift b/Queue.playground/Sources/SingleLLNode.swift new file mode 100644 index 0000000..d39252a --- /dev/null +++ b/Queue.playground/Sources/SingleLLNode.swift @@ -0,0 +1,120 @@ +// +// SingleLList.swift +// LinkList +// +// Created by Zhaohui Xing on 10/26/18. +// Copyright © 2018 Zhaohui Xing. All rights reserved. +// +import Foundation + +public class CSingleLinkedListNode { + var m_Data: T? + var m_Next: CSingleLinkedListNode? + var m_Hashvalue : Int64 //The hash value for hash table + init() { + let m : Int64 = Int64(INT_MAX) + let v1 : Int64 = Int64.random(in: 0 ..< m) + let v2 : Int64 = Int64.random(in: 0 ..< m) + + m_Hashvalue = v1 ^ v2 + } + + public init(data: T) { + let m : Int64 = Int64(INT_MAX) + let v1 : Int64 = Int64.random(in: 0 ..< m) + let v2 : Int64 = Int64.random(in: 0 ..< m) + m_Hashvalue = v1 ^ v2 + + self.m_Data = data + } + + public func clone()-> CSingleLinkedListNode { + let cloneNode : CSingleLinkedListNode = CSingleLinkedListNode(data: self.data!) //reference type "cloneNode", avoid "mutated warning for var" + //cloneNode.next = ((m_Next!) ?? m_Next?.clone() , nil) + /*guard let cloneNode.next = self.m_Next?.clone() else + { + print("Catched the source next nil") + cloneNode.next = nil + }*/ + if m_Next != nil + { + cloneNode.next = self.m_Next!.clone() + } + return cloneNode + } +} + +extension CSingleLinkedListNode { + public var data : T? { + return m_Data + } +} + +extension CSingleLinkedListNode { + public var next : CSingleLinkedListNode? { + set (newNode) { + m_Next = newNode + } + get { + return m_Next + } + } +} + +extension CSingleLinkedListNode { + public var tail : CSingleLinkedListNode? { + let ret = iscycled() + if ret.retFlag { + return ret.tailNode + } + else { + if m_Next != nil { + if m_Next?.tail != nil { + return m_Next?.tail + } + else { + return m_Next + } + } + else { + return self; + } + } + } +} + +extension CSingleLinkedListNode { //: Hashable { + public var hashValue: Int64 { + return m_Hashvalue + } +} + + +extension CSingleLinkedListNode { + public func iscycled()->(retFlag : Bool, cycleNode:CSingleLinkedListNode?, tailNode:CSingleLinkedListNode?) { + var bRet : Bool = false + var cycleNode:CSingleLinkedListNode? + var tailNode:CSingleLinkedListNode? + + var hashLut = [Int64: CSingleLinkedListNode]() //hash table by Dictionary + var cur : CSingleLinkedListNode? = self; + + cycleNode = nil + tailNode = nil + + while cur != nil { + if cur!.next != nil && hashLut[cur!.next!.hashValue] != nil { + cycleNode = cur!.next! + tailNode = cur! + bRet = true + break + } + hashLut[cur!.hashValue] = cur + cur = cur!.next + } + + return (bRet, cycleNode, tailNode); + } +} + + diff --git a/Queue.playground/contents.xcplayground b/Queue.playground/contents.xcplayground new file mode 100644 index 0000000..a93d484 --- /dev/null +++ b/Queue.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Queue.playground/playground.xcworkspace/contents.xcworkspacedata b/Queue.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Queue.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Queue.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Queue.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Queue.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Queue.playground/playground.xcworkspace/xcuserdata/zhaohuixing.xcuserdatad/UserInterfaceState.xcuserstate b/Queue.playground/playground.xcworkspace/xcuserdata/zhaohuixing.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..5aa21bc Binary files /dev/null and b/Queue.playground/playground.xcworkspace/xcuserdata/zhaohuixing.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Queue.playground/timeline.xctimeline b/Queue.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/Queue.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Queue.playground/xcuserdata/zhaohuixing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Queue.playground/xcuserdata/zhaohuixing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..560f4a4 --- /dev/null +++ b/Queue.playground/xcuserdata/zhaohuixing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,23 @@ + + + + + + + + + diff --git a/Stack.playground/Contents.swift b/Stack.playground/Contents.swift index c43e239..2e1a569 100644 --- a/Stack.playground/Contents.swift +++ b/Stack.playground/Contents.swift @@ -1,7 +1,3 @@ -//import Cocoa - -var str = "Hello, playground" - typealias CStackInt = CStack let testStack : CStackInt = CStackInt() @@ -15,11 +11,14 @@ testStack.Push(value: 6) testStack.Push(value: 7) testStack.Push(value: 8) +print(testStack.Count()) + while testStack.Empty() == false { let v = testStack.Peek() print(v ?? "v is nil") let v2 = testStack.Pop() print(v2 ?? "v2 is nil") + print(testStack.Count()) } diff --git a/Stack.playground/Sources/CSingleLList.swift b/Stack.playground/Sources/CSingleLList.swift index 08916bd..49b6d69 100644 --- a/Stack.playground/Sources/CSingleLList.swift +++ b/Stack.playground/Sources/CSingleLList.swift @@ -123,6 +123,18 @@ extension CSingleLinkedList { } } +extension CSingleLinkedList { + public func Append(value: CSingleLinkedListNode) { + if self.head == nil { + self.head = value + } + else { + m_Tail?.next = value + m_Tail = value + } + } +} + extension CSingleLinkedList { public func reverseBetween(start startIndex: Int, end endIndex: Int) { if endIndex <= startIndex { @@ -134,7 +146,7 @@ extension CSingleLinkedList { //tempPrev is the start node to swap sequence. var tempPrev : CSingleLinkedListNode? //"?" allowing following calling "tempPrev = nil", otherwise compiling error for it tempPrev = nil - + let steps : Int = (0 < startIndex) ? startIndex-1 : 0 for _ in 0...steps { if cur != nil { @@ -159,12 +171,12 @@ extension CSingleLinkedList { } if tempPrev != nil { - tempPrev?.next = tempHead + tempPrev?.next = tempHead } else { m_Head = tempHead } - + } } diff --git a/Stack.playground/Sources/CStack.swift b/Stack.playground/Sources/CStack.swift index a23a6fc..6065932 100644 --- a/Stack.playground/Sources/CStack.swift +++ b/Stack.playground/Sources/CStack.swift @@ -60,3 +60,10 @@ extension CStack { return ret } } + +extension CStack { + public func Count()->Int { + let ret : Int = m_Container.count + return ret + } +} diff --git a/Stack.playground/playground.xcworkspace/xcuserdata/zhaohuixing.xcuserdatad/UserInterfaceState.xcuserstate b/Stack.playground/playground.xcworkspace/xcuserdata/zhaohuixing.xcuserdatad/UserInterfaceState.xcuserstate index 4089b99..8bb476d 100644 Binary files a/Stack.playground/playground.xcworkspace/xcuserdata/zhaohuixing.xcuserdatad/UserInterfaceState.xcuserstate and b/Stack.playground/playground.xcworkspace/xcuserdata/zhaohuixing.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Stack.playground/timeline.xctimeline b/Stack.playground/timeline.xctimeline index dd8e270..ce5afcb 100644 --- a/Stack.playground/timeline.xctimeline +++ b/Stack.playground/timeline.xctimeline @@ -3,22 +3,22 @@ version = "3.0">