代码拉取完成,页面将自动刷新
/**
* 单向链表
*/
// 封装
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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。