本文详细介绍如何在JavaScript中封装单向链表数据结构。通过定义Node类管理节点数据与指针,构建LinkList类并实现append、insert、remove等核心方法,完整展示链表的创建、遍历、增删改查及状态判断逻辑,为前端开发提供基础数据结构实现参考。
单向链表封装实现
以下是利用JavaScript语言实现单向链表封装的具体方案。
第一步,定义LinkList类,该类的实例用于表示整个链表结构。
第二步,在LinkList内部定义Node类,用于封装每个节点的数据信息,包含data属性存储数据值,以及next属性指向下一个节点。
第三步,在LinkList实例中维护两个关键属性:length属性记录链表当前包含的节点数量,head属性指向链表中的第一个节点。
第四步,为LinkList类封装一系列常用的操作方法:
- append(element)方法:用于在列表的尾部添加一个新的元素。
- insert(position, element)方法:用于在列表指定的位置插入一个新的元素。
- get(position)方法:用于获取指定位置的元素值。
- indexOf(element)方法:返回指定元素在链表中的索引位置;若链表中不存在该元素,则返回-1。
- update(position, element)方法:用于修改指定位置的元素值。
- removeAt(position)方法:用于从列表的指定位置移除一个元素。
- remove(element)方法:用于从列表中移除第一个匹配的元素。
- isEmpty()方法:判断链表是否为空,若链表中不包含任何元素则返回true,否则返回false。
- size()方法:返回链表中当前包含的元素个数。
- toString()方法:将链表中的所有元素值转换为字符串并输出。
<script type="text/javascript">
function LinkList(){
/* 节点类 */
function Node(data){
this.data = data
this.next = null
}
this.head = null
this.length = 0
/* 追加方法 */
LinkList.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
}
/* toString方法 */
LinkList.prototype.toString = function(){
var current = this.head
var listString = ""
while(current){
listString += current.data +" "
current = current.next
}
return listString
}
/* insert方法 */
LinkList.prototype.insert = function(position,data){
/* 对position进行越界判断 */
if(position<0||position>this.length) return false
var node = new Node(data)
if(position == 0){
node.next = this.head
this.head = node
}else{
var index = 0
var current = this.head
var previous = null
while(index++ < position){
previous = current
current = current.next
}
node.next = current
previous.next = node
}
this.length += 1
return true
}
/* get方法 */
LinkList.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
}
/* indexOf方法 */
LinkList.prototype.indexOf = function(data){
/* 定义变量 */
var current = this.head
var index = 0
/* 开始查找 */
while(current){
if(current.data === data){
return index
}else{
current = current.next
index += 1
}
}
return -1
}
/* update方法 */
LinkList.prototype.update = function(position,data){
/* 越界判断 */
if(position<0 || position >= this.length) return false
var current = this.head
var index = 0
while(index++ < position){
current = current.next
}
/* 修改data */
current.data = data
return true
}
/* removeAt方法 */
LinkList.prototype.removeAt = function(position){
/* 越界判断 */
if(position<0 || position >= this.length) return null
var current = this.head
if(position === 0){
this.head = this.head.next
}else{
var index = 0
var previous = null
while(index++ < position){
previous = current
current = current.next
}
previous.next = current.next
}
this.length -= 1
return current.data
}
/* remove */
LinkList.prototype.remove = function(data){
/* 根据data找位置 */
var position = this.indexOf(data)
return this.removeAt(position)
}
LinkList.prototype.isEmpty = function(){
return this.length === 0
}
LinkList.prototype.size = function(){
return this.length
}
}
/* 测试 */
var list = new LinkList()
list.append('a')
list.append('b')
list.append('c')
console.log(list.toString()) /* a b c */
list.insert(3,'d')
console.log(list.toString())/* a b c d */
console.log(list.get(2)) /* c */
console.log(list.indexOf('d')) /* 3 */
list.update(1,'bbb')
console.log(list.toString()) /* a bbb c d */
console.log(list.removeAt(2)) /* c */
console.log(list.toString())/* a bbb d */
console.log(list.remove('a'))
console.log(list.toString())/* bbb d */
console.log(list.isEmpty()) /* false */
console.log(list.size()) /* 2 */
</script>
以上即为JavaScript封装单向链表的示例代码的详细内容,更多关于JavaScript封装单向链表的资料请关注本站其它相关文章!