HTML编程 双向列表,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())

效果如下:

7d9a670d8431ead0d14e1befcda8ec19.png

753e5f0934341d599f6febbbbb9328fe.png

d1358d4695d8660de2972cc1f6e682b2.png

Leisure_Time

发布了15 篇原创文章 · 获赞 9 · 访问量 245

私信

关注

标签:head,posi,JavaScript,length,链表,tail,let,双向,data

来源: https://blog.csdn.net/weixin_45615791/article/details/103963712

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值