_跑堂的 2011-12-17 20:16
浏览 307
已采纳

模拟一个HashTable类,包含有add、remove、contains、length方法

[size=medium]如题,求教:模拟一个HashTable类,包含add、remove、contains、length方法?[/size]

  • 写回答

2条回答 默认 最新

  • _1_1_7_ 2011-12-19 08:43
    关注

    [quote]
    [code="js"]
    function Hashtable()
    {
    this.container = new Object();

            /**//** put element */
            this.put = function (key, value) 
            {
                if (typeof (key) == "undefined")
                {
                    return false;
                } 
                if (this.contains(key))
                {
                    return false;
                } 
                this.container[key] = typeof (value) == "undefined" ? null : value;
                return true;
            };
    
            /**//** remove element */
            this.remove = function (key) 
            {
                delete this.container[key];
            };
    
            /**//** get size */
            this.size = function () 
            {
                var size = 0;
                for (var attr in this.container) 
                {
                    size++;
                }
                return size;
            };
    
            /**//** get value by key */
            this.get = function (key) 
            {
                return this.container[key];
            };
    
            /**//** containts a key */
            this.contains = function (key) 
            {
                return typeof (this.container[key]) != "undefined";
            };
    
            /**//** clear all entrys */
            this.clear = function () 
            {
                for (var attr in this.container)
                {
                    delete this.container[attr];
                }
            };
    
            /**//** hashTable 2 string */
            this.toString = function()
            {
                var str = "";
                for (var attr in this.container)
                {
                    str += "," + attr + "=" + this.container[attr];
                }
                if(str.length>0)
                {
                    str = str.substr(1, str.length);
                }
                return "{" + str + "}";
            };
        }
    

    [/code]
    [/quote]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?