简体   繁体   English

Javascript:链接列表:无法删除对象引用

[英]Javascript: Linked List: Unable to delete object reference

I'm doing a Linked List data structure. 我正在做一个Linked List数据结构。 The prototype includes a method to pop (delete) the last item from the list which I'm attempting to do by finding the last object, and then setting it to null . 原型包括一个方法来弹出(删除)列表中的最后一项,我试图通过查找最后一个对象,然后将其设置为null It does not seem to work. 它似乎不起作用。 What does work is setting the reference (the 'pointer') in the previous object to null . 将上一个对象中的引用(“指针”)设置为null工作原理是什么。 I'm still a relative JS OOP newbie, can't get my brain to understand why. 我仍然是一个相对的JS OOP新手,无法让我的大脑明白为什么。 The code: 编码:

function LinkedList() {
    this._rootNode = null;
    this._length = 0;                                       
}

LinkedList.prototype = {

   push: function(data) {
       var newNode = {
           data: data,                                      
           nextNode: null
       };
       // initialize this._rootNode or subsequent .nextNode with newNode
       this._length++;
   },

   pop: function() {
       var selectedNode, perviousNode;

       if ( this._rootNode ) {
           if ( this._length > 1 ) {
               selectedNode = this._rootNode;
               while ( selectedNode.nextNode ) {
                   previousNode = selectedNode;  // <-- shouldn't need this?
                   selectedNode = selectedNode.nextNode;
               }
               selectedNode = null;              // <-- doesn't delete it
               // previousNode.nextNode = null;  // <-- works (but feels unnecessary?)
           } else {  
               this._rootNode = null;
           }
           this._length--;
       }
   },

   // more methods..
};


/* --- Main Prorgam --- */

var list = new LinkedList();

list.push('AAA');
list.push('BBB');
list.pop();
console.log(list._rootNode.nextNode.data);   <-- 'BBB' still there

Would appreciate some insight, and any other tips on improving the function. 非常感谢有关改进功能的一些见解和其他任何提示。 Thanks! 谢谢!

I guess you realize that your push method doesn't work, but you haven't asked about that one. 我猜你知道你的push方法不起作用,但你没有问过那个。

If you are doing some kind of school project that requires you to write a linked list like this, then by all means, continue. 如果你正在做某种学校项目,要求你写这样的链表,那么一定要继续。 Your issue is that selectedNode is not really "the node itself", it's a reference to it, and you're just setting that reference to null while the previous item's nextNode pointer still refers to it, so you haven't actually removed it from your list. 你的问题是selectedNode实际上并不是“节点本身”,它是对它的引用,而你只是将该引用设置为null,而前一项的nextNode指针仍引用它,所以你实际上没有将它从你的清单。 You would actually do so by un-commenting the line setting that pointer to null, which means you also have to leave in the line saving the reference to the previous node. 实际上,您可以通过取消注释指针为null的行设置来执行此操作,这意味着您还必须保留对前一节点的引用的行。

previousNode.nextNode = null;

You actually don't want to delete the node entirely with pop() , you want to return it. 实际上你不想用pop()完全删除节点,你想要返回它。 Once you remove the reference to the popped node in your calling function though, it will be the last reference and the object will be made available for garbage collection. 删除对调用函数中弹出节点的引用后,它将是最后一个引用,该对象将可用于垃圾回收。 This is (to my knowledge) how all traditional OOP languages handle linked lists at the basic level. 这是(据我所知)所有传统的OOP语言如何处理基本级别的链表。

Which brings me to my next point, that most OOP languages you'll use these days don't actually require you to work on the basic level. 这让我想到了下一点,你现在使用的大多数OOP语言实际上并不需要你在基础级别上工作。 Most of them have libraries that will implement linked lists for you and Javascript in particular essentially implements a linked list-style data structure in its array syntax. 他们中的大多数都有为您实现链表的库,而Javascript特别实现了数组语法中的链表样式数据结构。 To the point where ([1,2,3,4]).pop() evaluates to 4 and ([1,2,3,4]).push(5) evaluates to [1,2,3,4,5] . ([1,2,3,4]).pop()计算结果为4([1,2,3,4]).push(5)计算结果为[1,2,3,4,5] If you actually need to USE a linked list in a real project, just don't. 如果您确实需要在实际项目中使用链接列表,那就不要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM