diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..243da55 Binary files /dev/null and b/.DS_Store differ diff --git a/Stack.playground/Contents.swift b/Stack.playground/Contents.swift new file mode 100644 index 0000000..c43e239 --- /dev/null +++ b/Stack.playground/Contents.swift @@ -0,0 +1,25 @@ +//import Cocoa + +var str = "Hello, playground" + +typealias CStackInt = CStack + +let testStack : CStackInt = CStackInt() + +testStack.Push(value: 1) +testStack.Push(value: 2) +testStack.Push(value: 3) +testStack.Push(value: 4) +testStack.Push(value: 5) +testStack.Push(value: 6) +testStack.Push(value: 7) +testStack.Push(value: 8) + +while testStack.Empty() == false { + let v = testStack.Peek() + print(v ?? "v is nil") + let v2 = testStack.Pop() + print(v2 ?? "v2 is nil") + +} + diff --git a/Stack.playground/Sources/CSingleLList.swift b/Stack.playground/Sources/CSingleLList.swift new file mode 100644 index 0000000..08916bd --- /dev/null +++ b/Stack.playground/Sources/CSingleLList.swift @@ -0,0 +1,170 @@ +// +// 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 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/Stack.playground/Sources/CStack.swift b/Stack.playground/Sources/CStack.swift new file mode 100644 index 0000000..a23a6fc --- /dev/null +++ b/Stack.playground/Sources/CStack.swift @@ -0,0 +1,62 @@ +// +// CStack.swift +// A stack class implemented by single linkedin list class CSingleList +// +// Created by Zhaohui Xing on 01/02/19. +// Copyright © 2019 Zhaohui Xing. All rights reserved. +// +import Foundation + +public class CStack { + let m_Container: CSingleLinkedList + + //Make default initializer public to be accessible + public init() { + m_Container = CSingleLinkedList() + } +} + +extension CStack { + public func Push(value: T) { + let newHead : CSingleLinkedListNode = CSingleLinkedListNode(data:value) + m_Container.AddHead(head: newHead) + } +} + +extension CStack { + public func Pop()->T? { + var tempValue : T? //"?" allowing following calling "tempValue = nil", otherwise compiling error for it + tempValue = nil + + if m_Container.head != nil { + tempValue = m_Container.head?.data + m_Container.RemoveHead() + } + + return tempValue + } +} + +extension CStack { + public func Peek()->T? { + var tempValue : T? //"?" allowing following calling "tempValue = nil", otherwise compiling error for it + tempValue = nil + + if m_Container.head != nil { + tempValue = m_Container.head?.data + } + + return tempValue + } +} + +extension CStack { + public func Empty()->Bool { + var ret : Bool = true + + if m_Container.head != nil { + ret = false + } + return ret + } +} diff --git a/Stack.playground/Sources/SingleLLNode.swift b/Stack.playground/Sources/SingleLLNode.swift new file mode 100644 index 0000000..d39252a --- /dev/null +++ b/Stack.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/Stack.playground/contents.xcplayground b/Stack.playground/contents.xcplayground new file mode 100644 index 0000000..a93d484 --- /dev/null +++ b/Stack.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Stack.playground/playground.xcworkspace/contents.xcworkspacedata b/Stack.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Stack.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Stack.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Stack.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Stack.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Stack.playground/playground.xcworkspace/xcuserdata/zhaohuixing.xcuserdatad/UserInterfaceState.xcuserstate b/Stack.playground/playground.xcworkspace/xcuserdata/zhaohuixing.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..4089b99 Binary files /dev/null and b/Stack.playground/playground.xcworkspace/xcuserdata/zhaohuixing.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Stack.playground/timeline.xctimeline b/Stack.playground/timeline.xctimeline new file mode 100644 index 0000000..dd8e270 --- /dev/null +++ b/Stack.playground/timeline.xctimeline @@ -0,0 +1,26 @@ + + + + + + + + + + + + +