swift算法手记-10

Posted 麦好

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift算法手记-10相关的知识,希望对你有一定的参考价值。

http://blog.csdn.net/myhaspl

    private func findnode(val:Int)->Bool{//http://blog.csdn.net/myhaspl

        //查找结点http://blog.csdn.net/myhaspl

        if let mysltop = slinktop{
            var mynode:skipLinkNode=mysltop
            while true{
                while true{
                    if let nextnd = mynode.nextnode {
                       let nodeval = nextnd.ndval
                       if  nodeval < val{
                           mynode=nextnd
                           continue
                        }
                        if nodeval == val{
                           return true
                        }
                    }
                    break
                }
                if mynode.downnode == nil{
                   return false
                }
                else{
                    mynode = mynode.downnode!
                }
            }
        }
        else{
            return false
        }

    }
    ....
    ....
....

   
    
    private func deletenode(val:Int){
        if let mysltop=slinktop{
            var mynode:skipLinkNode=mysltop
            while true{
                while true{
                    if let nextnd = mynode.nextnode {
                        let nodeval = nextnd.ndval
                        if  nodeval < val{
                            mynode=nextnd
                            continue
                        }
                        if nodeval == val{
                            //delete node from the level
                            mynode.nextnode=nextnd.nextnode
                        }
                    }
                    break
                }
                if mynode.downnode == nil{
                    //最底层http://blog.csdn.net/myhaspl

                    break
                }
                else{
                    mynode = mynode.downnode!
                }
            }
        }
    }


    private func insertnode(val:Int){
        //插入结点
        let insertlv=getinsertlv()
        let currtop=currlist(insertlv)
        var mynode:skipLinkNode = currtop

        var isfind:Bool=false
        var searchnodes=[(skipLinkNode,skipLinkNode)]()
        
        while true{
            while let ntnode=mynode.nextnode{
                if ntnode.ndval < val {
                    mynode = ntnode
                }
                else if ntnode.ndval == val {
                    isfind=true
                    searchnodes.append((ntnode,ntnode.nextnode!))
                    break
                }
                else{
                    searchnodes.append((mynode,ntnode))
                    break
                }
            }
            if let dnnode=mynode.downnode {
                mynode=dnnode
            }
            else{
                break
            }
        }

        
        var newnd:skipLinkNode?
        var upnd:skipLinkNode?
        var dnnd:skipLinkNode?
        var prend:skipLinkNode
        var ntnd:skipLinkNode
        if !isfind {
            for nodes in searchnodes{
                (prend,ntnd)=nodes
                upnd=newnd
                newnd=createnode(prend,nextnd:ntnd,nodetype: ntype.node,val:val)
                if upnd != nil{
                    upnd!.downnode=newnd
                }
                else{
                    dnnd = newnd!
                }
            }
            if insertlv>slinklevel
            {
                addnewlevel(val,dnnode: dnnd!)
            }
        }
        else{
            let nodelist=searchnodes.last
            (prend,ntnd)=nodelist!
            newnd=createnode(prend,nextnd:ntnd,nodetype: ntype.node,val:val)
        }

    }
    
    private func slinkstatus()->String{
        var mystatus:String=""
        var nownode:skipLinkNode
        
        var i=slinklevel
        while i>=0{
            nownode=slist[i]
            mystatus+="||top=>"
            while true{
                if let ntnode=nownode.nextnode {
                    if ntnode.ndtype != ntype.end {
                       mystatus+=String(ntnode.ndval)+"--"
                    }
                    else{
                       mystatus+="==>end||"
                    }
                    nownode=ntnode
                }
                else{
                    break
                }
            }
            mystatus += "\\n"
            i-=1
        }
        return mystatus
    }
    
    

本博客所有内容是原创,如果转载请注明来源

http://blog.csdn.net/myhaspl/


以上是关于swift算法手记-10的主要内容,如果未能解决你的问题,请参考以下文章

swift算法手记-7

swift算法手记-8

swift算法手记-5

swift手记-3

10分钟看懂10大经典算法(Swift代码实现)

swift常用代码片段