【问题标题】:data structure association list - how to create head key value pairs?数据结构关联列表 - 如何创建头键值对?
【发布时间】:2016-11-22 15:01:16
【问题描述】:

我有一个关于数据结构关联列表/单链表的问题,它只会添加到头部。 set 函数假设设置(多个)键值对,get 函数应该获取这些对 - 我不明白如何制作头部(最初假设为 null) 成为一个对象,并且由于新创建的节点成为“新”头-我不明白如何使用其键值对“移动”“旧”头.. 对任何帮助感到高兴!谢谢!

这是我的代码(不多,但根本不知道如何从这里开始)

function List () {
 this.head=null;
}

function ListN (key, value, next) {
  this.key = key;
  this.value = value;
  this.next = next;
}
Alist.prototype.set = function (key, value) {
  // this.key=value;
  var newNode=new ListN(key, value);
  this.head=newNode;
};

Alist.prototype.get = function (key) {
  return this.key;
};

smallList = new List();

【问题讨论】:

    标签: javascript data-structures singly-linked-list


    【解决方案1】:

    你快到了。您在 new ListN 的调用中错过了前一个节点。

    var newNode = new ListN(key, value, this.head);
    //                                  ^^^^^^^^^
    

    function List() {
        this.head = null;
    }
    
    List.prototype.set = function (key, value) {
    
        function ListN(key, value, next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    
        var node = this.head;
        while (node) {
            if (node.key === key) {
                node.value = value;
                return;
            }
            node = node.next;
        }
        this.head = new ListN(key, value, this.head);
    };
    
    List.prototype.get = function (key) {
        var node = this.head;
        while (node) {
            if (node.key === key) {
                return node.value;
            }
            node = node.next;
        }
    };
    
    var smallList = new List();
    
    smallList.set('one', 'abc');
    console.log(smallList);
    smallList.set('two', 'def');
    console.log(smallList);
    
    console.log(smallList.get('one'));
    console.log(smallList.get('two'));
    console.log(smallList.get('three')); 
    
    smallList.set('two', 'xyz');
    console.log(smallList);

    【讨论】:

    • 谢谢妮娜!现在设置多个键值对有效,但没有得到它们。设置/获取一个(不是多个)键值对也不起作用 - 不知道为什么,但我得到了 undefined ..
    • 非常感谢 - 现在一切正常。所以这里发生的事情(我正在学习数据结构,所以只是想理解它)是首先使用 new 关键字“创建”头部,然后我们询问头部是否为空或已创建(=具有值),如果是我们问它是否是同一个键,如果是,我们返回然后通过将其设置为下一个来移动头部 - 这是正确的吗?另外,我们需要返回头部,否则,如果我们确实将头部设置为下一个并且没有删除“前一个”,它会丢失吗?
    • 正确。但是存在一个问题,您只能获得具有相同键的第一个值。要更新或创建,您必须先搜索它,以防止重复。
    【解决方案2】:

    在键值对象中,您应该始终拥有一个键,因此请使用 KISS 原则:

    var object = {};
    
    object['aKey'] = 'some value';
    object['otherKey] = 'other value';
    

    如果您想要存储对象,请使用数组:

    var myArray = [];
    
    myArrray.push({'key': 'value'});
    myArrray.push({'key': 'value'});
    myArrray.push({'key1': 'value1'});
    

    如果你想要一个键有很多值:

    var object = {};
    
    if(!object.hasOwnProperty('aKey')){
      object['aKey'] = [];
    }
    
    object['aKey'].push('value');
    

    Javascript 很简单,所以保持简单:)

    【讨论】:

    • 这是一个关于数据结构关联列表的练习,所以我不能像其他方式那样只使用 js。
    猜你喜欢
    • 2021-09-22
    • 2019-01-04
    • 2018-11-24
    • 2020-09-10
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    相关资源
    最近更新 更多