我正在尝试用javascript编写一个链接列表。我有一个类、一个构造函数和一个addtoLast函数,它提供了彼此之间的连接。
但在addtoLast函数中,我无法访问“
下一个
“我的任何对象的属性。
上面写着
无法在编号“x”上创建属性“next”
(x作为第一个值和链表的标题)
代码为:
class LinkedList
{
constructor()
{
this.head=[];
this.next=null;
this.length=0;
}
addtoLast(value)
{
if(this.head==null)
{
this.head=value;
this.length++;
}
else
{
let now=this.head;
let newNode=value;
while(now.next!=null)
now=now.next;
now.next=newNode; //it gives that error
newNode.next=null; //and it gives too!
this.length++;
}
}
}
//and my main function is:
let example = new LinkedList();
example.head = 3;
example.addtoLast(9);
document.write(example);
如有任何评论,我将不胜感激:)