美文网首页
hashmap的js实现

hashmap的js实现

作者: infi_ | 来源:发表于2017-11-23 18:07 被阅读0次
function LinkedList(){
    var Node=function(element){
        this.element=element;
        this.next=null
    }
    var length=0
    var head=null
    this.append=function(element){
          var node=new Node(element)
          var current=null;
          if(head==null){
            head=node
          }else{
             current=head;
             while(current.next){
                current=current.next
             }
             current.next=node


          }
          length++
    }
    this.insert=function(position,element){
          if(position>-1&&position<=length){
            var node=new Node(element)
            var current=head;
            var previous;
            var index=0;

             if(position===0){
                node.next=head
                head=node
             }else{
                while(index++<position){
                    previous=current
                    current=current.next
                }
                node.next=current
                previous.next=node
             }
             length++
             return true
            }else{
                 return false
             }
    }
    this.removeAt=function(position){
        if(position>-1&&position<length){
                var current=head;
                var previous;
                var index=0;
               if(position==0){
                head=current.next;
               }else{
                  while(index++<position){
                      previous=current
                      current=current.next
                      }
                   previous.next=current.next   

               }
               length--
           }else{
            return null
           }
    }
    this.remove=function(element){
          var index=this.indexOf(element)
          return this.removeAt(index) 

    }
    this.indexOf=function(element){
          var current=head;
          var index=0
          while(current){
            if(element==current.element){
               return index
               }
               index++
               current=current.next
            }
            return -1

    }

    this.isEmpty=function(){

        return length===0
    }
    this.size=function(){

        return length
    }
    this.getHead=function(){
         return head
    }
    this.toString=function(){
         var current=head;
         var string=''
         while(current){
            string+=(current.element+',')
            current=current.next
         }

         return string

    }
   
}
var ValuePair=function(key,value){
    this.key=key
    this.value=value
    this.toString=function(){
        return '['+this.key+'-'+this.value+']'
    }
}





  function hashTable(){
     var table=[]
     var loseloseHashCode=function(key){
        var hash=0
        for(var i=0;i<key.length;i++){
            hash+=key.charCodeAt(i)
        }
        return hash%37
     }
     this.put=function(key,value){
        var position=loseloseHashCode(key)      //由于可能有冲突 这里的position作为每条链表的总key  链表元素每个element有key 和value
        if(table[position]==undefined){
            table[position]=new LinkedList()
        }
        table[position].append(new ValuePair(key,value))
     }
     this.get=function(key){
        var position=loseloseHashCode(key)
        if(table[position]!=undefined){
             var current=table[position].getHead()
             while(current.next){
                if(current.element.key===key){
                    return current.element.value
                }
                current=current.next
             }
             if(current.element.key===key){  //只有一个元素的情况下
                return current.element.value
             }

        }
        return undefined
     }
     this.remove=function(key){
        var position=loseloseHashCode(key)
        if(table[position]!==undefined){
             var current=table[position].getHead()
             while(current.next){
                if(current.element.key===key){
                    table[position].remove(current.element)
                    if(table[position].isEmpty()){
                        table[position]=undefined
                    }
                    return true
                }
                current=current.next
             }
             if(current.element.key===key){
                table[position].remove(current.element);
                if(table[position].isEmpty()){
                    table[position]=undefined
                }
                return true
             }

        }else{
            return false
        }
     }
     this.getAll=function(){
        return table
     }


  }
  var hash=new hashTable()
  hash.put("Tyrion","wangxu")
  hash.put("Tyrion","miaotiezheng")

    hash.put("Tyrion3","ppp")

  console.log(hash.getAll())
  console.log(hash.get("Tyrion"))

console.log(document.getElementsByTagName("*"))

在平常的生产过程中 特别是前端 用的地方不多 即便理解了 过段时间也很可能会忘 权当了解

相关文章

网友评论

      本文标题:hashmap的js实现

      本文链接:https://www.haomeiwen.com/subject/ptftbxtx.html