1 Star 0 Fork 0

math.most/js_study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
027单向链表.js 4.35 KB
一键复制 编辑 原始数据 按行查看 历史
math.most 提交于 2021-09-21 09:28 +08:00 . 补充
/**
* 单向链表
*/
// 封装
function LinkedList() {
// 1. 内部类
function Node(data) {
this.data = data
this.next = null
}
// 2. 属性
this.head = null
this.length = 0
// 3. append
LinkedList.prototype.append = function(data) {
var newNode = new Node(data)
if (this.length == 0) {
this.head = newNode
} else {
var current = this.head
while (current.next) {
current = current.next
}
current.next = newNode
}
this.length += 1
}
// 4. toString
LinkedList.prototype.toString = function() {
var current = this.head
var listString = ""
while (current) {
listString += current.data + " "
current = current.next
}
return listString
}
// 5. insert
LinkedList.prototype.insert = function(position, data) {
// 越界判断
if (position < 0 || position > this.length) return false
var newNode = new Node(data)
// 判断插入的节点是否为第一个
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
}
// length+1
this.length += 1
return true
}
// 6. get
LinkedList.prototype.get = function(position) {
// 越界判断
if (position < 0 || position >= this.length) return null
// 读取data
var current = this.head
var index = 0
while (index >= position) {
current = current.next
}
// 返回数据
return current.data
}
// 7. indexOf
LinkedList.prototype.indexOf = function(data) {
// 定义属性
var current = this.head
var index = 0
// 开始查找
while (current) {
if (current.data == data) {
return index
}
current = current.next
index ++
}
// 如果没有找到, 返回-1
return -1
}
// 8. update
LinkedList.prototype.update = function(position, newData) {
// 越界判断
if (position < 0 || position >= this.length) return false
// 定义属性
var current = this.head
var index = 0
while (index++ < position) {
current = current.next
}
current.data = newData
return true
}
// 9. removeAt
LinkedList.prototype.removeAt = function(position) {
// 越界判断
if (position < 0 || position >= this.length) return false
// 判断移除的节点是否是第一个
if (position == 0) {
this.head = this.head.next
} else {
var current = this.head
var index = 0
var previous = null
while (index++ < position) {
previous = current
current = current.next
}
previous.next = current.next
}
// length-1
this.length -= 1
return true
}
// 10. remove
LinkedList.prototype.remove = function(data) {
// 找到data的position
var position = this.indexOf(data)
// 移除元素
return this.removeAt(position)
}
// 11. isEmpty
LinkedList.prototype.isEmpty = function() {
return this.length == 0
}
// 12. size
LinkedList.prototype.size = function() {
return this.length
}
}
// 测试代码
var link = new LinkedList()
link.append('a')
link.append('b')
link.append('c')
link.append('d')
link.append('e')
link.append('f')
console.log(link.toString()) // a b c d e f
link.insert(0, 'g')
link.insert(3, 'h')
console.log(link.toString()) // g a b h c d e f
link.update(2, 'i')
link.update(4, 'j')
console.log(link.toString()) // g a i h j d e f
console.log(link.get(3)) // g
console.log(link.indexOf('e')) // 6
link.removeAt(3)
link.remove('e')
console.log(link.toString()) // g a i j d f
console.log(link.isEmpty()) // false
console.log(link.size()) // 6
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mathmost/js_study.git
git@gitee.com:mathmost/js_study.git
mathmost
js_study
js_study
master

搜索帮助