本文共 3597 字,大约阅读时间需要 11 分钟。
相对于数组,链表有一些优点:
相对于数组,链表有一些缺点:
function LinkedList(){ //内部类:节点类 function Node(data){ this.data = data this.next = null } //属性 this.head = null this.length = 0 //1.追加方法 LinkedList.prototype.append = function(data){ // 1.1 创建新的节点 var newNode = new Node(data) // 1.2判断是否添加的是第一个节点 if(this.length == 0){ // 2.1 是第一个节点 this.head = newNode } else { // 2.2 不是第一个节点 // 找到最后一个节点 var current = this.head while(current.next){ current = current.next } // 最后节点的next指向新节点 current.next = newNode } // 1.3 长度+1 this.length += 1 } // 2.toString方法 LinkedList.prototype.toString = function(){ // 2.1 定义变量 var current = this.head var listString = ' ' // 2.2 循环获取一个个节点 while(current){ listString += current.data + ' ' current = current.next } return listString }} // 3.insert方法 LinkedList.prototype.insert = function(position , data){ // 3.1 对position进行越界判断 if(position < 0 || position > this.length) return false // 此处为>,当position=length时,相当于在尾部追加 // 3.2 根据data创建newNode var newNode = new Node(data) // 3.3 判断插入的位置是否是第一个 if(position == 0){ newNode.next = this.head this.head = newNode } else { var index = 0 var current = this.head var previous = null while(index++ < position){ previous = current current = current.next } newNode.next = current previous.next = newNode } // 3.4 length+1 this.length += 1 return true } // 4.get方法 LinkedList.prototype.get = function(position){ // 4.1 越界判断 if(position < 0 || position >= this.length) return null //此处为>=,因为当position=length时,节点已经不存在了 // 4.2 获取对应的data var index = 0 var current = this.head while(index++ < position){ current = current.next } return current.data } // 5. indexOf方法 LinkedList.prototype.indexOf = function(data){ // 5.1 定义变量 var current = this.head var index = 0 // 5.2 开始查找 while(current){ if(current.data == data){ return index } current = current.next index += 1 } // 5.3 找到最后没有找到,返回-1 return -1 // 6.update方法 LinkedList.prototype.update = function(position, newData){ // 6.1 越界判断 if(position < 0 || position >= this.length) return false // 6.2 查找正确的节点 var current = this.head var index = 0 while(index++ < position){ current = current.next } // 6.3 将position位置的node的data修改成newData current.data = newData return true } // 7.removeAt方法 LinkedList.prototype.removeAt = function(position){ // 7.1 越界判断 if(position < 0 || position >= this.length) return null // 7.2 判断是否删除的是第一个节点 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 } // 前一个节点的next指向current的next即可 previous.next = current.next } // 7.3 length-1 this.length -= 1 return current.data } // 8. remove方法 LinkedList.prototype.remove = function(data){ // 8.1 获取data在链表中的位置 var position = this.indexOf(data) // 8.2 根据位置信息,删除节点 return this.removeAt(position) } }
转载地址:http://thze.baihongyu.com/