首頁>Club>
6
回覆列表
  • 1 # 使用者834195712159

    節點定義

    // 節點

    function Node(data, next){

    this.data = data;

    this.next = next;

    }

    連結串列定義

    // 連結串列

    function NodeList(node){

    this.length = 0;

    this.next = node;

    }

    1.頭插法

    // 建立連結串列(頭插法)

    function CreateListHead(num){

    var list = new NodeList(null);

    for(var i=0; i<num; i++){

    var node = new Node(Math.round(Math.random()* 100),null);

    console.info(node)

    node.next = list.next;

    list.next = node;

    list.length++;

    }

    return list;

    }

    2.尾插法

    // 建立連結串列(尾插法)

    function CreateListTail(num){

    var list = new NodeList(null);

    var p = list;

    for(var i=0; i<num; i++){

    var node = new Node(Math.round(Math.random()* 100),null);

    console.info(node)

    p.next = node;

    p = node;

    list.length++;

    }

    return list

    }

  • 中秋節和大豐收的關聯?
  • 第一次去男朋友家招冷落,是否該繼續?