js單向鏈表的具體實現(xiàn)實例
function linkNode(_key, _value)
{
/// <summary>
/// 鏈表類的節(jié)點類
/// </summary>
this.Key = _key;
this.Value = _value;
this.next = null;
}
function Link()
{
/// <summary>
/// 創(chuàng)建一個鏈表類
/// </summary>
this.root = new linkNode(null, null); //root永遠是個空節(jié)點
this.end = this.root;
}
Link.prototype =
{
count: 0,
value: function (_key)
{
/// <summary>
/// 根據(jù)key的值來獲取value值
/// </summary>
/// <param name="_key" type="String">
/// key的值
/// </param>
/// <returns type="Object">
/// 對應的value的值
/// </returns>
var i = this.root;
while (Boolean(i = i.next))
{
if (i.Key == _key)
return i.Value;
}
},
add: function (_key, _value)
{
/// <summary>
/// 往鏈表的尾部中加入一個節(jié)點
/// </summary>
/// <param name="_key" type="String">
/// key的值
/// </param>
/// <param name="_value" type="Object">
/// value的值
/// </param>
/// <returns type="Object">
/// 返回新增加的value的值
/// </returns>
var i = this.root;
while (Boolean(i = i.next))
{
if (i.Key == _key)
return i.Value = _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)
{
/// <summary>
/// 從鏈表類的某節(jié)點之后插入新節(jié)點node.
/// </summary>
/// <param name="_key" type="String">
/// 在鍵值等于_key的元素之后插入
/// </param>
/// <param name="node" type="Object">
/// 要插入的元素
/// </param>
var i = this.root;
while (Boolean(i = i.next))
{
if (i.Key == _key)
{
var tmp = i.next;
i.next = node;
node.next = tmp;
break;
}
}
},
insertBefore: function (_key, node)
{
/// <summary>
/// 從鏈表類的某節(jié)點之后插入新節(jié)點node.
/// </summary>
/// <param name="_key" type="String">
/// 在鍵值等于_key的元素之后插入
/// </param>
/// <param name="node" type="Object">
/// 要插入的元素 m.fzitv.net
/// </param>
var i = this.root;
while (Boolean(i = i.next))
{
if (i.next.Key == _key)
{
var tmp = i.next;
i.next = node;
node.next = tmp;
break;
}
}
},
remove: function (_key)
{
/// <summary>
/// 從鏈表類中移除一個key
/// </summary>
/// <param name="_key" type="String">
/// key的值
/// </param>
var i = this.root;
do
{
if (i.next.Key == _key)
{
if (i.next.next == null)
this.end = i;
i.next = i.next.next;
this.count--;
return;
}
} while (Boolean(i = i.next))
},
exists: function (_key)
{
/// <summary>
/// 檢查鏈表類中是否存在一個key
/// </summary>
/// <param name="_key" type="String">
/// key的值
/// </param>
/// <returns type="Boolean">
/// </returns>
var i = this.root;
while (Boolean(i = i.next))
if (i.Key == _key)
return true;
return false;
},
removeAll: function ()
{
/// <summary>
/// 清空鏈表類
/// </summary>
this.root = new linkNode(null, null);
this.end = this.root;
this.count = 0;
},
Obj2str: function (o)
{
if (o == undefined)
{
return "";
}
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")
{
if (!o.sort)
{
for (var i in o)
r.push("\"" + i + "\":" + this.Obj2str(o[i]));
r = "{" + r.join() + "}";
}
else
{
for (var i = 0; i < o.length; i++)
r.push(this.Obj2str(o[i]))
r = "[" + r.join() + "]";
}
return r;
}
return o.toString().replace(/\"\:/g, '":""');
},
getJSON: function ()
{
/// <summary>
/// 轉(zhuǎn)換成JSON字符串
/// </summary>
/// <returns type="String">
/// </returns>
//內(nèi)部方法,用于遞歸
var me = this;
var getChild = function (node)
{
var str = "";
str += "{\"Key\":\"" + node.Key + "\",\"Value\":" + me.Obj2str(node.Value);
if (node.next != null)
str += ",\"next\":" + getChild(node.next);
else
str += ",\"next\":\"null\"";
str += "}";
return str;
};
var link = "{\"root\":{\"Key\":\"null\",\"Value\":\"null\",\"next\":";
if (this.count == 0)//如果空表
{
return "{\"root\":{\"Key\":\"null\",\"Value\":\"null\",\"next\":\"null\"},\"end\":{\"Key\":\"null\",\"Value\":\"null\",\"next\":\"null\"},\"count\":\"0\"}";
}
link += getChild(this.root.next) + "}";
//加上end
link += ",\"end\":{\"Key\":\"" + this.end.Key + "\",\"Value\":" + me.Obj2str(this.end.Value) + ",\"next\":\"null\"";
link += "},\"count\":\"" + this.count + "\"}";
return link;
},
getArrayJSON: function ()
{
/// <summary>
/// 轉(zhuǎn)所有節(jié)點的value換成JSON字符串,數(shù)組格式
/// </summary>
/// <returns type="String">
/// </returns>
var link = "{\"link\":[";
var i = this.root;
while (Boolean(i = i.next))
{
link += this.Obj2str(i.Value) + ",";
}
link = link.substr(0, link.length - 1);
link += "]}";
return link;
},
sort: function (fn)
{
/// <summary>
/// 對鏈表進行排序
/// </summary>
/// <param name="fn" type="Function">
/// 比較兩個鏈表元素大小的方法,當返回真時,此方法的參數(shù)所指的節(jié)點將往下沉
/// </param>
if (fn != null)
{
var i = this.root;
while (Boolean(i = i.next))
{
var j = this.root;
while (Boolean(j = j.next))
{
if (j.next != null)
{
if (fn.call(this, j))
{
var Key = j.Key;
var Value = j.Value;
j.Key = j.next.Key;
j.Value = j.next.Value;
j.next.Key = Key;
j.next.Value = Value;
}
}
}
this.end = i;
}
}
else
{
}
}
};
- JavaScript 雙向鏈表操作實例分析【創(chuàng)建、增加、查找、刪除等】
- JavaScript將數(shù)組轉(zhuǎn)換為鏈表的方法
- JS中的算法與數(shù)據(jù)結構之鏈表(Linked-list)實例詳解
- JS實現(xiàn)的合并兩個有序鏈表算法示例
- 使用JavaScript實現(xiàn)鏈表的數(shù)據(jù)結構的代碼
- JavaScript數(shù)據(jù)結構之鏈表的實現(xiàn)
- javascript循環(huán)鏈表之約瑟夫環(huán)的實現(xiàn)方法
- JavaScript實現(xiàn)鏈表插入排序和鏈表歸并排序
- JS使用單鏈表統(tǒng)計英語單詞出現(xiàn)次數(shù)
- JavaScript封裝單向鏈表的示例代碼
相關文章
Jquery實現(xiàn)的tab效果可以指定默認顯示第幾頁
tab效果想必大家在網(wǎng)上都有見過很多吧,在本文將為大家介紹下如何實現(xiàn)可以在代碼里面指定默認顯示第幾頁的tab效果,感興趣的朋友不要錯過2013-10-10
解決layui上傳文件提示上傳異常,實際文件已經(jīng)上傳成功的問題
今天小編就為大家分享一篇解決layui上傳文件提示上傳異常,實際文件已經(jīng)上傳成功的問題。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
淺談nodeName,nodeValue,nodeType,typeof 的區(qū)別
本文主要簡單介紹了nodeName,nodeValue,nodeType,typeof 的區(qū)別,算是知識點的一個小總結,希望對小伙伴們有所幫助2015-01-01

