JavaScript封裝單向鏈表的示例代碼
使用JavaScript封裝單向鏈表:
1. 封裝LinkList的類,用于表示我們的鏈表結(jié)構(gòu)。
2. 在LinkList類中有一個(gè)Node類,用于封裝每一個(gè)節(jié)點(diǎn)上的信息(data與next)。
3. 在鏈表中保存兩個(gè)屬性,一個(gè)是鏈表的長度,一個(gè)是鏈表中的第一個(gè)節(jié)點(diǎn)。
4.封裝一些鏈表的常用方法:
- append(element):想列表尾部添加一個(gè)新的項(xiàng);
- insert(position,element):向列表的特定位置插入一個(gè)新的項(xiàng);
- get(position):獲取對(duì)應(yīng)位置的元素;
- indexOf(element):返回元素在鏈表中的索引,如果鏈表中沒有該元素則返回-1;
- update(position,element):修改某個(gè)位置的元素;
- removeAt(postion):從列表的特定位置移除一項(xiàng);
- remove(element):從列表中移除一項(xiàng);
- isEmpty():如果鏈表中不包含任何元素,返回true,否則返回false;
- size():返回鏈表中包含元素的個(gè)數(shù);
- toString():輸出鏈表元素的值;
<script type="text/javascript">
function LinkList(){
/* 節(jié)點(diǎn)類 */
function Node(data){
this.data = data
this.next = null
}
this.head = null
this.length = 0
/* 追加方法 */
LinkList.prototype.append = function(data){
/* 創(chuàng)建新節(jié)點(diǎn) */
var newNode = new Node(data)
if(this.length === 0){
this.head = newNode
}else{
/* 找到最后一個(gè)節(jié)點(diǎn) */
var current = this.head
while(current.next){
current = current.next
}
current.next = newNode
}
this.length += 1
}
/* toString方法 */
LinkList.prototype.toString = function(){
var current = this.head
var listString = ""
while(current){
listString += current.data +" "
current = current.next
}
return listString
}
/* insert方法 */
LinkList.prototype.insert = function(position,data){
/* 對(duì)position進(jìn)行越界判斷 */
if(position<0||position>this.length) return false
var node = new Node(data)
if(position == 0){
node.next = this.head
this.head = node
}else{
var index = 0
var current = this.head
var previous = null
while(index++ < position){
previous = current
current = current.next
}
node.next = current
previous.next = node
}
this.length += 1
return true
}
/* get方法 */
LinkList.prototype.get = function(position){
/* 越界判斷 */
if(position<0 || position >= this.length) return null
var current = this.head
var index = 0
while(index++ < position){
current = current.next
}
return current.data
}
/* indexOf方法 */
LinkList.prototype.indexOf = function(data){
/* 定義變量 */
var current = this.head
var index = 0
/* 開始查找 */
while(current){
if(current.data === data){
return index
}else{
current = current.next
index += 1
}
}
return -1
}
/* update方法 */
LinkList.prototype.update = function(position,data){
/* 越界判斷 */
if(position<0 || position >= this.length) return false
var current = this.head
var index = 0
while(index++ < position){
current = current.next
}
/* 修改data */
current.data = data
return true
}
/* removeAt方法 */
LinkList.prototype.removeAt = function(position){
/* 越界判斷 */
if(position<0 || position >= this.length) return null
var current = this.head
if(position === 0){
this.head = this.head.next
}else{
var index = 0
var previous = null
while(index++ < position){
previous = current
current = current.next
}
previous.next = current.next
}
this.length -= 1
return current.data
}
/* remove */
LinkList.prototype.remove = function(data){
/* 根據(jù)data找位置 */
var position = this.indexOf(data)
return this.removeAt(position)
}
LinkList.prototype.isEmpty = function(){
return this.length === 0
}
LinkList.prototype.size = function(){
return this.length
}
}
/* 測(cè)試 */
var list = new LinkList()
list.append('a')
list.append('b')
list.append('c')
console.log(list.toString()) /* a b c */
list.insert(3,'d')
console.log(list.toString())/* a b c d */
console.log(list.get(2)) /* c */
console.log(list.indexOf('d')) /* 3 */
list.update(1,'bbb')
console.log(list.toString()) /* a bbb c d */
console.log(list.removeAt(2)) /* c */
console.log(list.toString())/* a bbb d */
console.log(list.remove('a'))
console.log(list.toString())/* bbb d */
console.log(list.isEmpty()) /* false */
console.log(list.size()) /* 2 */
</script>
以上就是JavaScript封裝單向鏈表的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于JavaScript封裝單向鏈表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- JavaScript axios安裝與封裝案例詳解
- 深入淺析同源與跨域,jsonp(函數(shù)封裝),CORS原理
- vue.js管理后臺(tái)table組件封裝的方法
- 詳解JavaScript面向?qū)ο髮?shí)戰(zhàn)之封裝拖拽對(duì)象
- 原生js封裝無縫輪播功能
- 原生JS封裝vue Tab切換效果
- js實(shí)現(xiàn)封裝jQuery的簡單方法與鏈?zhǔn)讲僮髟斀?/a>
- js實(shí)現(xiàn)Element中input組件的部分功能并封裝成組件(實(shí)例代碼)
- JavaScript實(shí)現(xiàn)原型封裝輪播圖
- JavaScript緩動(dòng)動(dòng)畫函數(shù)的封裝方法
- javascript canvas封裝動(dòng)態(tài)時(shí)鐘
- 關(guān)于Jackson的JSON工具類封裝 JsonUtils用法
- 常用的前端JavaScript方法封裝
相關(guān)文章
用js實(shí)現(xiàn)簡單算法的實(shí)例代碼
下面小編就為大家?guī)硪黄胘s實(shí)現(xiàn)簡單算法的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
javascript+xml技術(shù)實(shí)現(xiàn)分頁瀏覽
基于web的技術(shù)中,分頁是一個(gè)老的不能再老的,但大家津津樂道的問題,隨著xml技術(shù)的日漸應(yīng)用,把xml應(yīng)用到分頁當(dāng)中,也是一種可能,當(dāng)然網(wǎng)上的教程很多,當(dāng)我都是看得稀里糊涂,索性自己寫一個(gè),與大家分享、指正。2008-07-07
JavaScript 閉包在封裝函數(shù)時(shí)的簡單分析
近才開始系統(tǒng)的研究js,對(duì)js的興趣源于對(duì)JQuery的應(yīng)用。之前只會(huì)用js做簡單的計(jì)算函數(shù),后來由于需要做特效,故接觸JQ,看著API,基本的特效都能完成,但相反,如果用js去實(shí)現(xiàn),估計(jì)自己很難寫得出來,所以下定決心系統(tǒng)的看看js。2009-11-11
Fundebug支持監(jiān)控微信小程序HTTP請(qǐng)求錯(cuò)誤的方法
這篇文章主要介紹了Fundebug支持監(jiān)控微信小程序HTTP請(qǐng)求錯(cuò)誤的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02

