<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <script src="js/lib/angular.min.js"></script>
    <script >
        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);
                console.log(position + '-' + key);
                table[position] = value;
            };
            
            this.get = function(key) {
                return table[loseloseHashCode(key)];
            };
            
            this.remove = function(key) {
                table[loseloseHashCode(key)] = undefined;
            };
        }
        
        var hash = new HashTable();
                hash.put('Gandalf', '[email protected]');
                hash.put('John', '[email protected]');
                hash.put('Tyrion', '[email protected]');
                
                console.log(hash.get('Gandalf'));
                console.log(hash.get('Loiane'));
                
                hash.remove('Gandalf');
                console.log(hash.get('Gandalf'));


    </script>

</head>
<body>

</body>
</html>

javascript散列表实现

相关文章: