Javascript 循环链表和在原型中更改对象属性时出错
我想用js制作循环链表。我这样做:Javascript 循环链表和在原型中更改对象属性时出错,javascript,properties,linked-list,prototype,Javascript,Properties,Linked List,Prototype,我想用js制作循环链表。我这样做: var node = { // make node name: '', score: '', next: null, previous: null } function CircularLinkedList(){ // Circular Linked List constructor this.head = null; } CircularLinkedList.prototype.push = function(name , score
var node = { // make node
name: '',
score: '',
next: null,
previous: null
}
function CircularLinkedList(){ // Circular Linked List constructor
this.head = null;
}
CircularLinkedList.prototype.push = function(name , score){
var head = this.head,
current = head,
previous = head,
node = {name: name, score: score, previous:null, next:null };
if(!head){ // if link list was empty
node.previous = node;
node.next = node;
this.head = node; // ****the problem is here**** line 18
}
else{
while(current && current.next){ // find last element in link list
previous = current;
current = current.next;
}
node.next = head;
node.previous = current;
head.previous = node;
current.next = node;
}
}
我在主文件中写道:
var dll = new CircularLinkedList();
dll.push('a',2);
dll.push('b',3);
当我在chrome中运行这段代码时,我什么也看不到,chrome保持连接。
例如,如果我将第18行(****问题在这里****)更改为
代码没有问题。我该怎么办?在循环中,推送新项目时不需要遍历列表
var CircularList=function(){
this.push=函数(值){
var newNode={value:value};
如果(这个标题){
newNode.next=this.head;
newNode.previous=this.head.previous;
this.head.previous.next=newNode;
this.head.previous=newNode;
}否则{
this.head=newNode;
newNode.next=newNode;
newNode.previous=newNode;
}
};
}
var cl=新的循环列表();
cl.push({name:“hello”});
cl.push({name:“good”});
cl.push({name:“sir”});
document.body.innerText=cl.head.value.name+“”+cl.head.next.value.name+“”+cl.head.previous.value.name代码>我们如何得到头部??我不能使用myVar.head
它返回空值谢谢。我用的是ajax,我拿不到头。我的问题解决了:)
this.head = "s"