JavaScript实现双向链表
作者:互联网
实现双向链表, 代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>双向链表</title>
</head>
<body>
<script type="text/javascript">
function DoublyList() {
//初始化
this.head = null
this.tail = null
this.length = 0
//类中类:
function Node(data) {
this.prve = null
this.data = data
this.next = null
}
//append方法
DoublyList.prototype.append = data => {
let node = new Node(data)
if(this.length === 0) {
this.head = node
this.tail = node
}else {
node.prve = this.tail
this.tail.next = node
this.tail = node
}
this.length +=1
}
//toString方法
DoublyList.prototype.toString = () => this.forwardString()
//forwardString方法
DoublyList.prototype.forwardString = () => {
let head = this.head
let str = ''
while(head) {
str += head.data + ' '
head = head.next
}
return str
}
//backwardString方法
DoublyList.prototype.backwardString = () => {
let tail = this.tail
let str = ''
while(tail) {
str += tail.data + ' '
tail = tail.prve
}
return str
}
//indexOf方法
DoublyList.prototype.indexOf = data => {
let index = 0
let head = this.head
while(head) {
if(head.data === data) {
return index
}else {
head = head.next
index +=1
}
}
return -1
}
//isEmpty方法
DoublyList.prototype.isEmpty = () => this.length === 0
//size方法
DoublyList.prototype.size = () => this.length
//getHead方法
DoublyList.prototype.getHead = () => this.head.data
//getTail方法
DoublyList.prototype.getTail = () => this.tail.data
//insert方法
DoublyList.prototype.insert = (posi, data) => {
//越界判断
if(posi < 0 || posi > this.length) throw Error('输入的数值越界'+posi+', 在insert方法')
//长度判断
if(this.length === 0) {
this.append(data)
}else {
if(posi === 0) {
let node = new Node(data)
let head = this.head
head.prve = node
node.next = head
head = node
this.length += 1
}else if(posi === this.length) {
this.append(data)
}else {
let node = new Node(data)
let head = this.head
for(let i = 0; i < posi; i++) {
head = head.next
}
node.next = head
node.prve = head.prve
head.prve.next = node
head.prve = node
}
this.length += 1
}
}
//get方法
DoublyList.prototype.get = posi => {
if(posi < 0 || posi > this.length) throw Error('输入的数值越界'+posi+', 在get方法')
if(this.length / 2 < posi) {
//如果大于一般, 则反向便利
let tail = this.tail
for(let i = this.length; i > posi; i--) {
tail = tail.prve
}
return tail.data
}else {
let head = this.head
for(let i = 0; i < posi; i++) {
head = head.next
}
return head.data
}
}
//indexOf方法
DoublyList.prototype.indexOf = data => {
let head = this.head
let index = 0
while(head) {
if(head.data === data) return index
index += 1
head = head.next
}
return -1
}
//updata方法
DoublyList.prototype.updata = (posi, data) => {
if(posi < 0 || posi > this.length) throw Error('输入的数值越界'+posi+', 在updata方法')
if(this.length / 2 < posi) {
//如果大于一般, 则反向便利
let tail = this.tail
for(let i = this.length; i > posi; i--) {
tail = tail.prve
}
tail.data = data
return true
}else {
//如果小于, 则正常便利
let head = this.head
for(let i = 0; i < posi; i++) {
head = head.next
}
head.data = data
return true
}
}
//removeAt方法
DoublyList.prototype.removeAt = posi => {
if(posi < 0 || posi >= this.length) throw Error('输入的数值越界'+posi+', 在removeAt方法')
if(this.length === 1) {
this.head = this.tail = null
}else {
if(posi === 0) {
this.head.next.prve = null
this.head = this.head.next
}else if(posi === (this.length -1)) {
this.tail.prve.next = null
this.tail = this.tail.prve
}else {
let head = this.head
for(let i = 0; i < posi; i++) {
head = head.next
}
head.prve.next = head.next
head.next.prve = head.prve
}
}
this.length -= 1
}
//remove方法
DoublyList.prototype.remove = data => {
//简单方法
let index = this.indexOf(data)
return this.removeAt(index)
//复杂方法
// let head = this.head
// let index = 0
// while(head){
// if(head.data === data) {
// //调用封装好的removeAt方法, 传入index
// this.removeAt(index)
// }
// index += 1
// head = head.next
// }
}
}
let list = new DoublyList()
list.append('添加1')
list.append('添加2')
list.append('添加3')
list.append('添加4')
list.append('添加5')
list.append('添加6')
list.insert(5, '向第四行添加数据')
console.log(list.get(7))
console.log(list.indexOf('添加7'))
list.updata(6, '更新')
list.removeAt(6)
list.removeAt(4)
list.remove('添加1')
console.log(list)
console.log(list.forwardString())
console.log(list.backwardString())
</script>
</body>
</html>
效果如下:
Leisure_Time 发布了15 篇原创文章 · 获赞 9 · 访问量 245 私信 关注标签:head,posi,JavaScript,length,链表,tail,let,双向,data 来源: https://blog.csdn.net/weixin_45615791/article/details/103963712