javascript散列表实现

时间:2024-01-13 21:04:08
<!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', 'gandalf@email.com');
                hash.put('John', 'johnsnow@email.com');
                hash.put('Tyrion', 'tyrion@email.com');

                console.log(hash.get('Gandalf'));
                console.log(hash.get('Loiane'));

                hash.remove('Gandalf');
                console.log(hash.get('Gandalf'));

    </script>

</head>
<body>

</body>
</html>

javascript散列表实现