个人博客地址:https://tao-yuhan.gitee.io/tyhanblog/
链表的优势
- 链表中的元素在内存中不必是连续的空间
- 链表的每一个元素由一个存储元素本身的节点和一个指向下一个元素的引用
- 链表在创建的时候不需要确定大小,大小可以无线延伸下去
单向链表的一些基本操作
链表得有一个head指向第一个节点,其次每个节点中有data和next,next指向下一个节点
- append(element): 在尾部添加一个新的项
- insert(position,ele): 向链表的特定位置插入一个新的项
- get(position): 获取对应位置的元素
- indexOf(ele): 返回元素在链表中的索引,如果没有该元素则返回-1
- update(position,ele): 修改某个位置的元素
- removeAt(position): 从列表的特定位置移除一项
- remove(ele): 从链表中移除一项
- isEmpty(): 是否为空
- size(): 链表长度
- toString(): 输出元素的值
链表操作的封装
链表的基本实现
function LinkedList() {
this.head = null
this.length = 0
function Node(data) {
this.data = data
this.next = null
}
}
1.append
LinkedList.prototype.append = function (data) {
let newNode = new Node(data)
if (this.length == 0) {
this.head = newNode
} else {
let current = this.head
while (current.next) {
current = current.next
}
current.next = newNode
}
this.length++
}
2.insert
LinkedList.prototype.insert = function (position, data) {
if (position < 0 || position > this.length) return false
let newNode = new Node(data)
if (position == 0) {
newNode.next = this.head
this.head = newNode
} else {
let index = 0
let current = this.head
while (index++ < position - 1) {
current = current.next
}
newNode.next = current.next
current.next = newNode
}
this.length++
return true
}
3.get
LinkedList.prototype.get = function (position) {
if (position < 0 || position >= this.length) return false
let str = ""
let index = 0
let current = this.head
while (index++ < position) {
current = current.next
}
str += current.data
return str
}
4.indexOf
LinkedList.prototype.indexOf = function (data) {
let index = 0
let arr = []
let current = this.head
while (index < this.length) {
if (current.data == data) {
arr.push(index)
}
current = current.next
index++
}
if (arr.length == 0) {
return -1
} else if (arr.length == 1) {
return arr[0]
} else {
return arr
}
}
5.update
LinkedList.prototype.update = function (position, data) {
if (position < 0 || position >= this.length) return false
let index = 0
let current = this.head
while (index++ < position) {
current = current.next
}
current.data = data
}
6.removeAt
LinkedList.prototype.removeAt = function (position) {
if (position < 0 || position >= this.length) return false
let index = 0
let current = this.head
if(position === 0) {
this.head = this.head.next
}else {
while (index++ < position - 1) {
current = current.next
}
let mid = current.next
current.next = mid.next
mid.next = null
}
this.length -= 1
return true
}
7.remove
LinkedList.prototype.remove = function(ele) {
const position = this.indexOf(ele)
console.log(position);
if(position === -1) {
return false
}else if(position instanceof Array) {
position.forEach((item) => {
this.removeAt(item)
this.remove(ele)
})
}else {
this.removeAt(position)
}
return true
}
8.toString
LinkedList.prototype.toString = function () {
let current = this.head
let listString = ""
while (current) {
listString += current.data + " "
current = current.next
}
return listString
}
完整代码实现
function LinkedList() {
this.head = null
this.length = 0
function Node(data) {
this.data = data
this.next = null
}
LinkedList.prototype.append = function (data) {
let newNode = new Node(data)
if (this.length == 0) {
this.head = newNode
} else {
let current = this.head
while (current.next) {
current = current.next
}
current.next = newNode
}
this.length++
}
LinkedList.prototype.toString = function () {
let current = this.head
let listString = ""
while (current) {
listString += current.data + " "
current = current.next
}
return listString
}
LinkedList.prototype.insert = function (position, data) {
if (position < 0 || position > this.length) return false
let newNode = new Node(data)
if (position == 0) {
newNode.next = this.head
this.head = newNode
} else {
let index = 0
let current = this.head
while (index++ < position - 1) {
current = current.next
}
newNode.next = current.next
current.next = newNode
}
this.length++
return true
}
LinkedList.prototype.get = function (position) {
if (position < 0 || position >= this.length) return false
let str = ""
let index = 0
let current = this.head
while (index++ < position) {
current = current.next
}
str += current.data
return str
}
LinkedList.prototype.update = function (position, data) {
if (position < 0 || position >= this.length) return false
let index = 0
let current = this.head
while (index++ < position) {
current = current.next
}
current.data = data
}
LinkedList.prototype.indexOf = function (data) {
let index = 0
let arr = []
let current = this.head
while (index < this.length) {
if (current.data == data) {
arr.push(index)
}
current = current.next
index++
}
if (arr.length == 0) {
return -1
} else if (arr.length == 1) {
return arr[0]
} else {
return arr
}
}
LinkedList.prototype.removeAt = function (position) {
if (position < 0 || position >= this.length) return false
let index = 0
let current = this.head
if(position === 0) {
this.head = this.head.next
}else {
while (index++ < position - 1) {
current = current.next
}
let mid = current.next
current.next = mid.next
mid.next = null
}
this.length -= 1
return true
}
LinkedList.prototype.remove = function(ele) {
const position = this.indexOf(ele)
console.log(position);
if(position === -1) {
return false
}else if(position instanceof Array) {
position.forEach((item) => {
this.removeAt(item)
this.remove(ele)
})
}else {
this.removeAt(position)
}
return true
}
}
|