链表:存储数据一般可以用数组或者链表,不同于数组的是,链表中的元素不必是连续的空间。链表中的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(指针)组成。单向链表只能从头部遍历到尾部或者从尾部遍历到头部。
单向链表封装:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>单向链表</title>
</head>
<body>
<script>
function LinkedList() {
//内部类:节点类
function Nodes(data) {
this.data = data
this.next = null
}
//属性
this.head = null
this.length = 0
//方法
// 1.链表尾部追加元素方法
LinkedList.prototype.append = function (data) {
// 根据新元素创建节点
var newNode = new Nodes(data)
// 判断原来链表是否为空
if (this.head === null) { // 链表尾空
this.head = newNode
} else { // 链表不为空
// 2.1.定义变量, 保存当前找到的节点
var current = this.head
while (current.next) {
current = current.next
}
// 2.2.找到最后一项, 将其next赋值为node
current.next = newNode
}
// 3.链表长度增加1
this.length++
}
//2.toString方法
LinkedList.prototype.toString = function () {
var current = this.head
var str = ""
while (current) {
str += current.data + "\xa0\xa0\xa0"
current = current.next
}
return str
}
//3.insert方法:插入节点
LinkedList.prototype.insert = function (position, data) {
//判断边界值
if(position < 0 || position > this.length) return null
//创建一个新的节点
var newNode = new Nodes(data)
var current = this.head
var previons = null
var index = 0
//判断是否为第一个
if(position == 0){
newNode.next = current
this.head = newNode
}else{
while(index++ < position){
previons = current
current = current.next
}
previons.next = newNode
newNode.next = current
}
this.length ++
return true
}
//4.get方法:根据位置获取节点
LinkedList.prototype.get = function(position){
// 判断边界值
if(position < 0 || position >= this.length) return null
var current = this.head
var index = 0
while(index++ < position){
current = current.next
}
return current.data
}
//5.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++
}
return -1
}
// 6.update方法:更新替换指定位置节点
LinkedList.prototype.update = function (position, newData){
//边界判断
if(position >= this.length || position < 0) return false
var current = this.head
var index = 0
while(index++ < position){
current = current.next
}
current.data = newData
return true
}
// 7.removeAt方法:删除指定位置节点
LinkedList.prototype.removeAt = function (position){
// 越界判断
if(position >= this.length || position < 0) return false
//判断删除的是否是第一个
if(position == 0){
this.head = this.head.next
}else{
var current = this.head
var previons = null
var index = 0
while(index++ < position){
previons = current
current = current.next
}
previons.next = current.next
}
this.length--
return current.data
}
// 8.remove方法:根绝data删除某个节点
LinkedList.prototype.remove = function(data){
var position = this.indexOf(data)
this.removeAt(position)
}
//9.查看是否为空
LinkedList.prototype.isEmpty = function () {
return this.length == 0
}
//10.查看元素的个数
LinkedList.prototype.size = function () {
return this.length
}
}
var list = new LinkedList()
list.append("asd")
list.append("aswe")
list.append("ewr")
list.insert(0,"aaa")
list.insert(2,"bbb")
list.insert(4,"ccc")
alert(list)
alert(list.size())
</script>
</body>
</html>
本文详细介绍了如何使用JavaScript实现单向链表,包括链表的基本结构、尾部追加、插入、获取、删除等方法,并展示了链表操作的实例。适合初学者理解链表数据结构及其应用。
319





