This article describes a method for referencing pointers in JavaScript.
Please try to complete the following cloze:
/* Create a queue with head head0, tail for tail0
/function Intlist (head0, tail0) {
this.head = head0 | | 0;
This.tail = Tail0 | | null;
}
/* Returns a intlist containing all the numbers in the array *
/intlist.list = function (__args) {
var sentinel = new Intlist (),
len = __ Args.length,
p;
p = Sentinel;
for (var i = 0; i < len; i++) {
p.tail = new Intlist (__args[i]);
p = p.tail;
}
return sentinel.tail;
};
/* Returns the string representation of the object *
/IntList.prototype.toString = function () {
var temp = "";
temp = "[";
for (var L = this; L!== null; L = l.tail) {
temp = temp + "" + L.head;
}
temp = "]";
return temp;
/** returns a intlist containing intlist A and intlist B, where
the elements of B are behind a. You cannot use the New keyword.
/function Dcatenate (A, B) {//
* Completing functions/
}
/** returns a new Intlist, which is Len,
* with # The start element is the beginning (where #0 is the first element),
* cannot change L.
/function sublist (L, start, Len) {/
* complete functions */
}
This is a linked list problem written in JavaScript. Because a linked list has a more complex reference operation, it can be used to examine the understanding of JavaScript references. Comes with simple test cases:
/* Test dcatenate and sublist functions correctly/function Test
() {
var A = Intlist.list ([4,6,7,3,8]),
B = Intlist.list ([ 3,2,5,9]);
Dcatenate (A, B);
if (a.tostring () = = "[4 6 7 3 8 3 2 5 9]") {
alert ("Dcatenate function is correct.") ");
} else{
alert ("Dcatenate function error.) ");
}
var L = Intlist.list ([3,4,5,2,6,8,1,9]), result
= Sublist (L, 3, 3);
if (result.tostring () = = "[2 6 8]") {
alert ("sublist function is correct. ");
} else{
alert ("sublist function is correct.) ");
}
}