数据结构_双向循环链表_tostring方法
//双向循环链表 class DoubleNode { constructor (data) { this.data = data this.prev = null this.next = null } } class DoubleCycleList { constructor () { this.head = null this.tail = null this.length = 0 } //追加数据 append (data) { let newNode = new DoubleNode(data) let current = this.head //判断是否为空 if (!this.head) { this.head = newNode this.tail = newNode this.head.prev = this.tail this.tail.next = this.head //否则在末尾添加数据 }else { newNode.prev = this.tail newNode.next = this.head this.head.prev = newNode this.tail.next = newNode this.tail = newNode } this.length++ return true } //插入数据 insert (posi, data) { let newNode = new DoubleNode(data) let current = this.head let index = 0 if (posi < 0 || posi > this.length || !Number.isInteger(posi)) return false if (posi == 0) { if (!this.head) { this.head = newNode this.tail = newNode this.head.prev = this.tail this.tail.next = this.head }else { newNode.next = this.head newNode.prev = this.tail this.head.prev = newNode this.tail.next = newNode this.head = newNode } }else if (posi == this.length) { newNode.prev = this.tail newNode.next = this.head this.head.prev = newNode this.tail.next = newNode this.tail = newNode }else { while (index++ < posi - 1) { current = current.next } newNode.next = current newNode.prev = current.prev current.prev.next = newNode current.prev = newNode } this.length++ return true } //查询 indexOf (data) { let current = this.head let index = 0 while (current != this.tail) { if (current.data === data) { return index }else { index++ current = current.next } } if (current.data === data) { return index } return -1 } //删除指定位置 removeAt (posi) { let current = this.head let index = 0 if (posi < 0 || posi > this.length || !Number.isInteger(posi)) return false if (posi == 0) { if (this.length == 1) { this.head = null this.tail = null }else { this.head.next.prev = this.tail this.tail.next = this.head.next this.head = this.head.next } }else if (this.length - 1 == posi) { this.tail.prev.next = this.head this.head.prev = this.tail.prev this.tail = this.tail.prev }else { while (index++ < posi -1) { current = current.next } current.next = current.next.next current.next.prev = current } this.length-- return true } remove (data) { return this.removeAt(this.indexOf(data)) } isEmpty () { return this.head == null } size () { return this.length } tostring () { let current = this.head let re = ‘‘ while (current != this.tail) { re += ‘,‘ + current.data current = current.next } re += ‘,‘+current.data return re.slice(1) } } let dList = new DoubleCycleList() dList.insert(0, 0) dList.append(1) dList.append(2) dList.append(3) dList.append(4) dList.insert(5, ‘number5‘)
相关推荐
koushr 2020-11-12
zhangxiafll 2020-11-13
kikaylee 2020-10-31
范范 2020-10-28
MILemon 2020-10-22
hugebawu 2020-10-12
LauraRan 2020-09-28
shenwenjie 2020-09-24
omyrobin 2020-09-23
guangcheng 2020-09-22
qiangde 2020-09-13
hanyujianke 2020-08-18
晨曦之星 2020-08-14
xiesheng 2020-08-06
KAIrving 2020-08-02
xiesheng 2020-08-02
范范 2020-07-30
chenfei0 2020-07-30