html数据链表,双向链表.html

/*

appent(data) 向列表尾部添加一个新的项

insert(data) 向列表特定位置插入一个新的项

get(index) 获取对应位置的元素

indexOf(data) 查找索引值

updata(index,data) 更新index上的data

removeAt(index) 从特定的位置上删除一项

remove(data) 从列表里面删除一项

isEmpty() 如果列表里面不包含任何元素的话 返回true 如果链表长度大于0则返回false

size() 返回链表的长度

toString() 遍历节点返回data值

forwardString() 返回正向遍历节点字符串形式

backwordString() 返回反向遍历节点字符串形式

*/

class DoublyLinkedList {

heda = null;

tail = null;

length = 0;

Node(data) {

const node = {

prev: null,

next: null,

data: data,

}

return node;

}

appent(data) {

let newData = this.Node(data);

if (this.length === 0) {

this.heda = newData;

} else {

this.tail.next = newData;

newData.prev = this.tail;

}

this.length += 1;

this.tail = newData;

}

insert(index, data) {

let newData = this.Node(data);

//越界判断 index从0开始 最大是等于length-1, 0的话是在head那里添加替换 length-1的话 最后一个添加

if (index > this.length || index < 0) return false;

if (index === 0) {

this.heda.prev = newData;

newData.next = this.heda;

this.heda = newData;

} else if (index === this.length) {

this.appent(data);

return

} else {

let current = this.heda;

for (let i = 0; i < index - 1; i++) {

current = current.next;

//查找到要插入项的父级

}

const son = current.next; // 要在他前面插入

son.prev = newData;

newData.prev = current;

current.next = newData;

newData.next = son;

}

this.length += 1;

}

removeAt(index) {

//越界判断

if (index < 0 || index > this.length) return false;

let current = this.heda;

let delData = null; //返回删除的项

if (this.length === 1) {

delData = this.heda;

this.heda = null;

this.tail = null;

} else {

if (index === 0) {

delData = this.heda;

current.next.prev = null;

this.heda = current.next;

} else if (index === this.length - 1) {

this.tail.prev.next = null;

// 最后一项的父级的next指向变成空

this.tail = this.tail.prev;

// tail的指向变为原来的最后一项的父级

} else {

for (let i = 0; i < index - 1; i++) {

current = current.next;

//得到父级

}

delData = current.next;

current.next.next.prev = current;

current.next = current.next.next;

}

}

this.length--;

return delData;

}

indexOf(data) {

if (this.length === 0) return false;

let current = this.heda;

for (let i = 0; i < this.length; i++) {

if (current.data === data) {

return i;

} else {

current = current.next;

}

}

return -1;

}

remove(data) {

return this.removeAt(this.indexOf(data));

}

get(index) {

if (index < 0 || index > this.length) return false;

if (index === 0) {

return this.heda;

} else {

// 判断index 是在前半截还是后半截 然后不同的操作

if (index <= (this.length - 1) / 2) {

let current = this.heda;

for (let i = 0; i < index; i++) {

current = current.next;

}

return current;

} else {

let current = this.tail;

for (let i = 0; i < this.length - index - 1; i++) {

current = current.prev

}

return current

}

}

}

updata(index, data) {

this.get(index).data = data;

}

forwardString() {

if (this.length < 0) return false;

let current = this.heda;

let str = ''

while (current) {

str += `${current.data} `;

current = current.next;

}

return str;

}

backwordString() {

if (this.length < 0) return false;

let current = this.tail;

let str = ''

while (current) {

str += `${current.data} `;

current = current.prev;

}

return str;

}

}

const fun = new DoublyLinkedList;

fun.appent(123);

fun.appent(456);

fun.appent(789);

fun.insert(0, 1);

fun.insert(4, 1111);

fun.updata(0, 2);

console.log(fun.get(4));

console.log(fun);

一键复制

编辑

Web IDE

原始数据

按行查看

历史

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值