链接列表上的 Typescript 垃圾收集
我目前正在学习打字稿并尝试实现链接列表。我遇到了一个场景,我不确定如何处理引用以及何时允许垃圾收集器收集一块内存。
节点实现
class Node{
next: Node | null;
data: number;
constructor(data: number){
this.data = data;
this.next = null;
}
}
列表实现
class LinkedList {
head: Node | null;
constructor(){
this.head = null;
}
// append
// deleteValue
deleteValue(value: number){
let prev: Node;
let curr: Node;
// loop until curr.data = value
// < Insert Deletion Code Here >
当我删除 curr
节点时,是否足以将 prev.next
指向 curr.next
从而消除对的所有引用curr
允许垃圾收集器捕获。或者我还必须设置 curr = null 吗?
I am currently learning typescript and tried my hand at implementing a Linked List. I came across a scenario in which I am not sure how references are handled and when the garbage collector is allowed to collect a piece of memory.
Node implementation
class Node{
next: Node | null;
data: number;
constructor(data: number){
this.data = data;
this.next = null;
}
}
List Implementation
class LinkedList {
head: Node | null;
constructor(){
this.head = null;
}
// append
// deleteValue
deleteValue(value: number){
let prev: Node;
let curr: Node;
// loop until curr.data = value
// < Insert Deletion Code Here >
When I am deleting the curr
node, is it enough to point prev.next
to curr.next
thereby eliminating all references to curr
allowing the garbage collector to capture. Or must I also set curr = null
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论