How to reference pointers in JavaScript

Source: Internet
Author: User
Tags reference

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.) ");
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.