單向鍊錶JS實現

2021-06-05 15:43:26 字數 1351 閱讀 3334

可以排序,增加,查詢,刪除的單向鍊錶

function linknode(_key, _value)

function link()

link.prototype =

},add: function (_key, _value)

var node = new linknode(_key, _value);

if (this.count == 0)

this.root.next = node;

else

this.end.next = node;

this.end = node;

this.count++;

return _value;

},insert: function (_key, node)}},

insertbefore: function (_key, node)}},

remove: function (_key)

} while (boolean(i = i.next))

},exists: function (_key)

,removeall: function ()

,obj2str: function (o)

var r = ;

if (typeof o == "string")

return "\"" + o.replace(/([\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";

if (typeof o == "object")";}

else

return r;

}return o.tostring().replace(/\"\:/g, '":""');

},getjson: function ()

";return str;

};var link = ",\"end\":,\"count\":\"0\"}";

}link += getchild(this.root.next) + "}";

//加上end

link += ",\"end\":,\"count\":\"" + this.count + "\"}";

return link;

},getarrayjson: function ()

link = link.substr(0, link.length - 1);

link += "]}";

return link;

},sort: function (fn)}}

this.end = i;}}

else}};

JS實現單向鍊錶 雙向鍊錶 迴圈鍊錶

鍊錶儲存有序的元素的集合,但是和陣列不同的是,鍊錶中的元素在記憶體中的儲存並不是連續的。每乙個鍊錶元素都包含了乙個儲存元素本身的節點和乙個指向下乙個元素的引用。看起來就像這樣 相對於傳統的陣列,鍊錶的乙個好處就是增刪的時候無需移動其它元素,只要更改指標的指向就可以了。但是缺點就是如果想要訪問鍊錶中的...

實現單向鍊錶

鍊錶類 public class link 增加節點 public void add node node else 輸出節點 public void print else 內部搜尋節點的方法 public boolean search string data 直到不存在下乙個節點結束搜尋 if th...

實現單向鍊錶

鍊錶類 public class link 增加節點 public void add node node else 輸出節點 public void print else 內部搜尋節點的方法 public boolean search string data 直到不存在下乙個節點結束搜尋 if th...