For more information about using Javascript reference pointers, see the following link:
The Code is as follows:
/* Create a queue with head0 header and tail0 tail */
Function IntList (head0, tail0 ){
This. head = head0 | 0;
This. tail = tail0 | null;
}
/* Return an IntList containing all the numbers in the array */
IntList. list = function (_ args ){
Var sentinel = new IntList (),
Len = _ args. length,
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 this 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 an IntList, including IntList A and IntList B,
* The Element B is behind. You cannot use the new keyword.
*/
Function dcatenate (A, B ){
/* Complete the function */
}
/** Return a new IntList whose length is len,
* Starting with the # start Element (where #0 is the first element ),
* L cannot be changed.
*/
Function sublist (L, start, len ){
/* Complete the function */
}
This is a linked list question written in Javascript. Because the linked list has more complex reference operations, it can be used to evaluate the understanding of Javascript references. With simple test cases:
The Code is as follows:
/* Test whether the dcatenate and sublist functions are correct */
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 ("The dcatenate function is correct. ");
} Else {
Alert ("dcatenate function error. ");
}
Var L = IntList. list ([,]),
Result = sublist (L, 3, 3 );
If (result. toString () = "[2 6 8]") {
Alert ("sublist function is correct. ");
} Else {
Alert ("sublist function is correct. ");
}
}
Javascript reference?
In fact, when you assign a reference type instance to a variable, the variable stores the reference of the instance:
Var temp = new Object ();
This kind of performance is very suitable for its name, the reference type, its instance is of course to reference.
When you pay the variable to another variable, you only copy the referenced variable:
Var temp2 = temp;
So although the definition: temp2 = temp, but they did not directly contact, for example, modified the reference of temp:
The Code is as follows:
Var temp = {
Name: "temp"
};
Var temp2 = temp;
Temp = {
Name: "not temp2"
};
Temp = temp2; // false
Of course, if we modify only the instance to which the Pointer Points, then temp2 is still equal to temp:
The Code is as follows:
Var temp = {
Name: "temp"
};
Var temp2 = temp;
Temp. name = "also temp2 ";
Temp = temp2; // true
What is IntList?
Let's analyze:
Create two empty variables, so L and Q are empty in the right figure. Create a new IntList whose header is 3 and whose tail is null. Assign the value referenced by L to Q, so both L and Q point to this new IntList. Q points to a newly created IntList whose header is 42 and its tail is null. Then, the Q pointer is assigned to L. tail, so that the two IntList are nested.
It can be seen that IntList is a data structure that uses pointers to achieve multiple embedded data structures. It is called a Linked List ).
1. Create two empty variables, so L and Q are empty in the right figure.
2. Create a New IntList whose header is 3 and whose tail is null. Assign the value referenced by L to Q, so both L and Q point to this new IntList.
3. Q points to a newly created IntList whose header is 42 and its tail is null. Then, the Q pointer is assigned to L. tail, so that the two IntList are nested.
It can be seen that IntList is a data structure that uses pointers to achieve multiple embedded data structures. It is called a Linked List ).
IntList merge
We only need to point the tail of one of them to the other. In this way, the two IntList are connected:
The Code is as follows:
/** Returns an IntList, including IntList A and IntList B,
* The Element B is behind. You cannot use the new keyword.
*/
Function dcatenate (A, B ){
Var p;
For (p = A; p! = Null; p = p. tail ){
If (p. tail = null ){
P. tail = B;
Break;
}
}
Return
}
IntList Interception
Because the question requirement cannot change the original IntList, we can only retrieve the data from the original IntList and recreate a new data.
The Code is as follows:
/** Return a new IntList whose length is len,
* Starting with the # start Element (where #0 is the first element ),
* L cannot be changed.
*/
Function sublist (L, start, len ){
Var K,
P,
J;
Var I = 0,
End = start + len;
For (P = L; I <= end; P = P. tail, I ++ ){
If (I <start ){
Continue;
} Else if (I === start ){
K = new IntList (P. head );
J = K;
} Else if (I> start & I <end ){
J. tail = new IntList (P. head );
J = J. tail;
} Else if (I> = end ){
Break;
}
}
Return K;
}
Questions
1. How do I pass parameters through a function? For example, what is the reference process of the following code?
The Code is as follows:
Var obj = {
Name: "anything"
};
Function getName (_ obj ){
Return _ obj. name;
}
Var name = getName (obj );