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 }
这里元素不多,体现不出优势,如果元素足够多,这种索引结构就能体现出优势来了。