日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

【javascript】数据结构-链表

發(fā)布時(shí)間:2025/3/14 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【javascript】数据结构-链表 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
// 創(chuàng)建一個(gè)鏈表 function LinkedList(){// 創(chuàng)建一個(gè)Node輔助類,表示需要加入列表的項(xiàng),它包含一個(gè)element屬性,即表示需要加入到列表中的值,next屬性表示指向下一個(gè)節(jié)點(diǎn)項(xiàng)目的指針let Node = function(element){this.element = element;this.next = null;};// 長(zhǎng)度初始化為0,列表頭部初始化為空l(shuí)et length = 0;let head = null;// append方法,向鏈表尾部追加元素this.append = function(element){let node = new Node(element),current;// 列表中的第一個(gè)節(jié)點(diǎn)if(head == null){head = node;}else{current = head;// 循環(huán)列表,直到找到最后一項(xiàng)while(current.next){current = current.next;}// 找到最后一項(xiàng),將next值賦給node,建立鏈接current.next = node;}// 更新列表長(zhǎng)度length++;};// insert方法,向列表的特定位置插入一個(gè)新的項(xiàng)this.insert = function(position, element){// 檢查越界值if(position >=0 && position <=length){// current是對(duì)插入元素之后的元素的引用,previous是對(duì)插入元素之前的元素的引用let node = new Node(element),current = head,previous,index = 0;// 在第一項(xiàng)位置添加if(position === 0){node.next = current;head = node;}else{while(index++ < position){previous = current;current = current.next;}node.next = current;previous.next =node;}// 更新列表長(zhǎng)度length++;return true;}else{return false;}};// removeAt方法:從鏈表特定位置移除一項(xiàng)this.removeAt = function(position){// 檢查越界值if(position > -1 && position < length){let current = head,previous,index = 0;// 移除第一項(xiàng)if(position === 0){head = current.next;}else{while(index++ < position){previous = current;current = current.next;}// 將previous與current的下一項(xiàng)鏈接起來(lái),跳過(guò)current從而移除它previous.next = current.next;}// 更新列表長(zhǎng)度length--;return current.element;}else{return null;}};// remove方法:從列表中移除一項(xiàng)this.remove = function(element){let index = this.indexOf(element);// 調(diào)用removeAt()方法return this.removeAt(index);};// indexOf方法:返回元素在列表中的索引,如果沒(méi)有該元素則返回-1;this.indexOf = function(element){let current = head,index = 0;while(current){if(element === current.element){return index;}index++;current = current.next;}return -1;};// isEmpty方法,如果鏈表中不包含任何元素,則返回true,否則返回falsethis.isEmpty = function(){return length === 0;};// size方法,返回鏈表中包含的元素個(gè)數(shù),與數(shù)組的length屬性類似this.size = function(){return length;};// getHead方法:返回鏈表第一個(gè)元素this.getHead = function(){return head;};// toSting方法,由于鏈表使用了Node類,重寫了javascript的toString方法,讓其只輸出元素的值this.toString = function(){let current = head,string = '';while(current){string += current.element + (current.next ? ',':'');current = current.next;}return string;};// print方法,用于在控制臺(tái)輸出鏈表元素this.print = function(){console.log(this.toString());}; }// 鏈表的使用 var lnkst = new LinkedList();// 打印鏈表長(zhǎng)度 console.log(lnkst.size());// 給鏈表添加元素 lnkst.append(1); lnkst.append(2); lnkst.append(3); lnkst.append(4); lnkst.append(5);// 調(diào)用打印方法 lnkst.print(); //輸出1,2,3,4,5// 插入元素 lnkst.insert(2,'a'); lnkst.print(); //輸出1,2,a,3,4,5// 按位置刪除元素 lnkst.removeAt(2); lnkst.print(); //輸出1,2,3,4,5// 按值刪除元素 lnkst.remove(5); lnkst.print(); //輸出1,2,3,4//判斷是否為空 console.log(lnkst.isEmpty()); //false// 獲取頭部; console.log(lnkst.getHead()); //1

  

轉(zhuǎn)載于:https://www.cnblogs.com/dragonir/p/7705005.html

總結(jié)

以上是生活随笔為你收集整理的【javascript】数据结构-链表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。