チェーンテーブルの作成、遍歴、挿入、削除、検索、更新

3763 ワード

以下の内容はkotlin言語に基づいて、説明はすべて注釈の中で、すべて私自身の理解で、間違いの地方はまたどうぞよろしくお願いします
  • チェーンテーブル
  • を作成
    //    
    class Node{
        var id:Int? = null
        var next:Node? = null
    }
    
  • 初期化チェーンテーブル
  • //     
    fun createLinkedList():Node?{
        var head:Node? = null //   
        var newnode:Node //         
        var last:Node = Node() //   
        for (i in 0 until 10){
            //      
            newnode = Node()
            newnode.id = i + 10086
            if (i == 0){//     ,    ->         
                head = newnode
            }else{//    ,         next ->       
                last.next = newnode
            }
            last = newnode//  last       
        }
        //       ,          next null,  next           null ,       
        last.next = null
        return head
    }
    
  • チェーンテーブル
  • を巡る
    //    
    fun Node?.loopThroughLinkedList(){
        var tmp:Node? = this
        if (tmp == null){
            return
        }else{
            print("Current:$tmp  Id:${tmp.id} Next:${tmp.next}
    ") tmp = tmp.next } tmp.loopThroughLinkedList() }
  • チェーンテーブル長
  • を取得する.
    //      
    fun Node?.linkedListLength():Int{
        if (this == null){
            return  0
        }
        var length:Int = 0
        var tmp:Node? = this
        while (tmp?.next != null){
            tmp = tmp?.next
            length += 1
        }
        length += 1//           
        return length
    }
    
  • 挿入
  • //  
    fun Node?.insert(insertNode: Node,index:Int):Node?{
        val length = this.linkedListLength()
        var tmpNode:Node? = this
    
        if (index < 0 || index > length-1){
            throw IllegalArgumentException("      ")
        }
        if (index == 0){//    
            insertNode.next = tmpNode//1、          
            tmpNode = insertNode//2、           
            return  tmpNode
        }else if (index == length-1){//    
            while (tmpNode?.next != null){//1、 tmpNode          while  
                tmpNode = tmpNode.next
            }
            tmpNode?.next = insertNode//2、             
            insertNode.next = null//3、             ,       next      null,          
            return this
        }else{//      
    
            var node:Node? = Node()
            for (i in 0 until (index - 1)){
                if (tmpNode?.next != null){
                    tmpNode = tmpNode.next//1、   n-1   
                }
            }
            //2、   n   
            tmpNode?.next = insertNode//3、  n-1          
            insertNode.next = node//4、            n   
            return this
        }
    }
    
  • 削除
  • //  
    fun Node?.delete(index:Int):Node?{
        val length = this.linkedListLength()
        var i = if (index >= length) length else index
        var tmp = this
        var pre:Node? = Node()
        for (i in 0 until index){
            if (tmp?.next != null){
                pre = tmp//             
                tmp = tmp.next//      
            }
        }
        if (tmp == this){//          
            return tmp?.next
        }
        pre?.next = tmp?.next//      
        tmp = null//        
        return this
    }
    
  • 検索
  • //  
    fun Node?.find(index: Int):Node?{
        var tmp = this
        val length = this.linkedListLength()
        if (index < 0 || index > length-1){
            throw IllegalArgumentException("      ")
        }
    
        for (i in 0 until index){
            if (tmp?.next != null){
                tmp = tmp.next
            }
        }
        return tmp
    }
    
  • 更新
  • //  
    fun Node?.update(index: Int,id:Int):Node?{
        var tmp = this
        val length = this.linkedListLength()
        if (index < 0 || index > length-1){
            throw IllegalArgumentException("      ")
        }
        for (i in 0 until index){
            if (tmp?.next != null){
                tmp = tmp.next
            }
        }
        tmp?.id = id
        return this
    }