亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

JavaScript封裝單向鏈表的示例代碼

瀏覽:56日期:2023-10-12 16:42:55

使用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 = datathis.next = null}this.head = nullthis.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.headwhile(current.next){current = current.next}current.next = newNode}this.length += 1}/* toString方法 */LinkList.prototype.toString = function(){var current = this.headvar 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 falsevar node = new Node(data)if(position == 0){node.next = this.headthis.head = node}else{var index = 0var current = this.headvar previous = nullwhile(index++ < position){previous = currentcurrent = current.next}node.next = currentprevious.next = node}this.length += 1return true}/* get方法 */LinkList.prototype.get = function(position){/* 越界判斷 */if(position<0 || position >= this.length) return nullvar current = this.headvar index = 0while(index++ < position){current = current.next}return current.data}/* indexOf方法 */LinkList.prototype.indexOf = function(data){/* 定義變量 */var current = this.headvar index = 0/* 開始查找 */while(current){if(current.data === data){return index}else{current = current.nextindex += 1}}return -1}/* update方法 */LinkList.prototype.update = function(position,data){/* 越界判斷 */if(position<0 || position >= this.length) return falsevar current = this.headvar index = 0while(index++ < position){current = current.next}/* 修改data */current.data = datareturn true}/* removeAt方法 */LinkList.prototype.removeAt = function(position){/* 越界判斷 */if(position<0 || position >= this.length) return nullvar current = this.headif(position === 0){this.head = this.head.next}else{var index = 0var previous = nullwhile(index++ < position){previous = currentcurrent = current.next}previous.next = current.next}this.length -= 1return 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}}/* 測試 */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)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 国产精品高清一区二区不卡 | 久久国产精品视频一区 | 亚洲永久视频 | 欧美黑人与白人做爰 | 成人伊人亚洲人综合网站222 | 亚洲一区二区在线免费观看 | 国产精品女同一区二区久久夜 | 亚洲精品福利一区二区三区 | 欧美激情在线播放第16页 | 91麻豆视频网站 | 日韩美aaa特级毛片 日韩美a一级毛片 | 国产成人v视频在线观看 | 国产视频在 | 国产麻豆视频在线看网站 | 成人黄色在线网站 | 爽爽爽爽爽爽a成人免费视频 | 在线播放人成午夜免费视频 | 国产不卡在线播放 | 香蕉视频在线观看免费国产婷婷 | 日韩中文字幕电影在线观看 | 国产刚刚发育被强j在线播放 | 手机免费看黄在线高清视频 | 在线网站你懂 | 成人三级在线 | 亚洲第一区在线观看 | 免费香蕉一区二区在线观看 | 特级一级毛片视频免费观看 | 精品国产综合区久久久久99 | 调教套上奶牛榨乳器喷奶水 | 91麻豆影视| 亚洲美女视频网 | 国产做爰免费视频观看 | 91在线观 | 美毛片 | 成人午夜免费视频免费看 | 免费看黄色小视频 | 日日摸夜夜添夜夜添破第一 | 免费看一级视频 | 日韩美女黄大片在线观看 | 免费看国产做爰大片 | 免费三级黄色片 |