介绍
链表和数组一样,都用来存储一系列的元素。但是链表和数组实现的机制完全不同。从而导致其相关操作的效率也有所不同。
链表的特点
- 链表是一种物理存储单元上非连续、非顺序的存储结构
- 链表直观形态,每个节点元素都有保存了下一个节点的引用,头部指向第一个节点,最后一个节点指向null。
链表和数组的区别
优点
- 链表存储空间是非连续的而数组的存储空间是连续的。因此链表的内存利用率会更高一点。
- 链表在头部插入元素和中间插入元素的效率会比数组的效率要高
- 链表在存储数据过程中无须确定链表的长度,而数组需要确定数组的长度,即使某些语言数组是可变长度的,其背后的原理其实也新建了长度更长的数组然后将其拷贝到新数组中最后返回新数组的引用。
缺点
- 链表查询效率比数组的查询效率要低很多因为他访问元素的过程中需要从头开始遍历,而数组可以通过下标直接访问。
链表的组成
- Head :指向链表的第一个节点
- Node:节点(包含着两项数据,一个是data用于存放当前节点的元素,一个是next用于存放下一个节点的引用)
- Length:记录链表的长度
链表的常见方法
增
- append(element)
- insert(position,element)
删
- remove(data)
- removeAt(position)
改
查
- get(position)
- indexOf(data)
- isEmpty()
- size()
- toString()
链表的实现
基本实现
- 实现内部函数Element 用户创建Element对象存储数据以及下一个节点的引用
- 实现append()方法完成追加元素的操作
- 实现toString()方法将链表中的元素转化为字符串的形式并返回
- 添加head属性用于存放链表的第一个节点
- 添加length属性用来记录链表中元素的个数
实现上述内容,链表就有了最基本功能和“初步的框架”
function Linklist (){
function Element (data){
this.data = data
this.next = null
}
this.head = null
this.length = 0
Linklist.prototype.append = function (value){
let ele = new Element(value)
let current = this.head
if (this.length == 0){
this.head = ele
}else {
while (current.next){
current = current.next
}
current.next = ele
}
this.length+=1
}
Linklist.prototype.toString = function (){
let current = this.head
let str = ""
while (current.next){
str+=`${current.data},`
current = current.next
}
str += `${current.data}`
return str
}
}
let linklist = new Linklist()
linklist.append("zhangsan")
linklist.append("lisi")
console.log(linklist.length)
console.log(linklist.toString())
运行结果
所有常用方法完的整实现
function Linklist (){
function Element (data){
this.data = data
this.next = null
}
this.head = null
this.length = 0
Linklist.prototype.append = function (value){
let ele = new Element(value)
let current = this.head
if (this.length == 0){
this.head = ele
}else {
while (current.next){
current = current.next
}
current.next = ele
}
this.length+=1
}
Linklist.prototype.toString = function (){
let current = this.head
let str = ""
while (current.next){
str+=`${current.data},`
current = current.next
}
str += `${current.data}`
return str
}
Linklist.prototype.isEmpty = function (){
return this.length == 0
}
Linklist.prototype.size = function (){
return this.length
}
Linklist.prototype.get = function (position){
if (position<0 || position >= this.length) return null
let current = this.head
for (let i = 0 ;i < position;i++){
current = current.next
}
return current.data
}
Linklist.prototype.insert = function (position,data){
if (position<0 || position>this.length) return false
let ele = new Element(data)
let current = this.head
if (position == 0){
ele.next = this.head
this.head = ele
}else {
let i = 0;
let previous = null;
while(i<position){
previous = current
current = current.next
i++
}
previous.next = ele
ele.next = current
}
this.length++
return true
}
Linklist.prototype.update = function (position,newData){
if (position<0 || position >= this.length) return false
let current = this.head
for (let i = 0;i < position;i++){
current = current.next
}
current.data = newData
return true
}
Linklist.prototype.indexOf = function (data){
let current = this.head
for (let i= 0;i<this.length;i++){
if (current.data == data){
return i
}
current = current.next
}
return -1
}
Linklist.prototype.removeAt = function (position){
if (position<0 || position >= this.length) return null
if (position == 0){
this.head = this.head.next
}
let current = this.head
let previous = null
for (let i = 0;i<position;i++){
previous = current
current = current.next
}
previous.next = current.next
this.length-= 1
return current.data
}
Linklist.prototype.remove = function (data){
let index = this.indexOf(data)
return this.removeAt(index)
}
}
console.log("*******第一阶段测试*******")
console.log()
var linklist = new Linklist()
console.log("是否为空:",linklist.isEmpty())
linklist.append("zhangsan")
linklist.append("lisi")
console.log("是否为空:",linklist.isEmpty())
console.log("length:",linklist.length)
console.log("size:",linklist.size())
console.log(linklist.toString())
console.log("==========================")
console.log("*******get测试*******")
console.log()
var linklist = new Linklist()
linklist.append("zhangsan")
linklist.append("lisi")
console.log(linklist.length)
console.log(linklist.toString())
console.log(linklist.get(-1));
console.log(linklist.get(0));
console.log(linklist.get(1));
console.log(linklist.get(2));
console.log("==========================")
console.log("*******insert测试*******")
console.log()
var linklist = new Linklist()
linklist.append("zhangsan")
linklist.append("lisi")
linklist.append("wangwu")
console.log(linklist.length)
console.log(linklist.toString())
console.log(linklist.insert(-1,"hhhh"))
console.log(linklist.insert(0,"zhaoliu"))
console.log(linklist.insert(1,"xiaoming"))
console.log(linklist.insert(5,"xiaodu"))
console.log(linklist.insert(7,"hhhh"))
console.log(linklist.toString())
console.log("==========================")
console.log("*******update测试*******")
console.log()
var linklist = new Linklist()
linklist.append("zhangsan")
linklist.append("lisi")
console.log(linklist.length)
console.log(linklist.toString())
console.log(linklist.update(0,"wangwu"))
console.log(linklist.toString())
console.log(linklist.update(1,"zhaoliu"))
console.log(linklist.toString())
console.log(linklist.update(-1,"haha"))
console.log(linklist.toString())
console.log(linklist.update(3,"xiaodu"))
console.log(linklist.toString())
console.log("==========================")
console.log("*******get测试*******")
console.log()
var linklist = new Linklist()
linklist.append("zhangsan")
linklist.append("lisi")
console.log(linklist.length)
console.log(linklist.toString())
console.log(linklist.indexOf("hh"));
console.log(linklist.indexOf("lisi"));
console.log("==========================")
console.log("*******removeAt测试*******")
console.log()
var linklist = new Linklist()
linklist.append("zhangsan")
linklist.append("lisi")
console.log(linklist.length)
console.log(linklist.toString())
console.log(linklist.removeAt(-1))
console.log(linklist.toString())
console.log(linklist.removeAt(2))
console.log(linklist.toString())
console.log(linklist.removeAt(1))
console.log(linklist.toString())
console.log("==========================")
console.log("*******remove测试*******")
console.log()
var linklist = new Linklist()
linklist.append("zhangsan")
linklist.append("lisi")
console.log(linklist.length)
console.log(linklist.toString())
console.log(linklist.remove("hhh"))
console.log(linklist.toString())
console.log(linklist.remove("lisi"))
console.log(linklist.toString())
console.log("==========================")
|