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

您的位置:首頁技術文章
文章詳情頁

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

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

使用JavaScript封裝單向鏈表:

1. 封裝LinkList的類,用于表示我們的鏈表結構。

2. 在LinkList類中有一個Node類,用于封裝每一個節點上的信息(data與next)。

3. 在鏈表中保存兩個屬性,一個是鏈表的長度,一個是鏈表中的第一個節點。

4.封裝一些鏈表的常用方法:

append(element):想列表尾部添加一個新的項; insert(position,element):向列表的特定位置插入一個新的項; get(position):獲取對應位置的元素; indexOf(element):返回元素在鏈表中的索引,如果鏈表中沒有該元素則返回-1; update(position,element):修改某個位置的元素; removeAt(postion):從列表的特定位置移除一項; remove(element):從列表中移除一項; isEmpty():如果鏈表中不包含任何元素,返回true,否則返回false; size():返回鏈表中包含元素的個數; toString():輸出鏈表元素的值;

<script type='text/javascript'>function LinkList(){/* 節點類 */function Node(data){this.data = datathis.next = null}this.head = nullthis.length = 0/* 追加方法 */LinkList.prototype.append = function(data){/* 創建新節點 */var newNode = new Node(data)if(this.length === 0){this.head = newNode}else{/* 找到最后一個節點 */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){/* 對position進行越界判斷 */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){/* 根據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封裝單向鏈表的示例代碼的詳細內容,更多關于JavaScript封裝單向鏈表的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 中文无码久久精品 | 欧美黄色片在线播放 | 九月婷婷开心九月 | 天堂毛片 | 成年人在线播放视频 | 国产永久免费高清在线观看视频 | 国产成人做受免费视频 | 国产短视频精品一区二区三区 | 欧洲美女粗暴交视频 | 国产在线观看免费 | 99九九精品视频 | 亚洲影音先锋看看 | 97欧美在线看欧美视频免费 | 99精选视频 | 久久中国| 国内一区二区三区精品视频 | 久久艹逼 | 一级片网站在线观看 | 国内精品综合九九久久精品 | 中国一级特黄特级毛片 | 国产亚洲欧美日韩在线观看一区二区 | 日韩一级a毛片欧美一级 | 一区二区三区四区在线视频 | 国产一级免费视频 | 亚洲精品另类 | 欧洲成品大片在线播放 | 欧美一级a俄罗斯毛片 | 黄网址在线观看 | 国产91丝袜在线播放九色 | 达达兔午夜起神影院在线观看麻烦 | 亚洲精品aⅴ一区二区三区 亚洲精品aⅴ中文字幕乱码 | 深夜做爰性大片中文 | 成人在线播放视频 | 欧美一级毛片做受 | 男女做污污无遮挡激烈免费 | 成人欧美在线视频 | 草草视频网站 | 2级毛片| 色播欧美 | 日韩成人在线观看视频 | a毛片免费全部播放完整成 a毛片免费观看 |