用js写出自定义单向链表以及常用功能

 封装了六个常用功能分别是

1、append(ele) 向链表中添加节点

2、insert(position, data) 指定位置插入新节点

3、remove(position) 移除指定位置的元素

4、indexOf(ele) 查询指定的的元素,若存在则返回当前位置即index,不存在则返回-1

5、remove(ele)  移除指定元素

6、toString() 将链表中的数据连接为字符串

​
// 节点的结构
			class Lnode {
				constructor(data) { //设置每一个节点的基本属性
					this.data = data;
					this.next = null;
				}
			}
			// 链表的结构
			class LinkList {
				constructor() {
					this.head = null; //头部
					this.length = 0; //记录个数
				}

				//1、向链表中添加节点
				append(ele) {
					// 创建新节点
					let newnode = new Lnode(ele); //每次创建的新节点的next都为null
					//如果原链表为空时,则直接将新节点保存到head中,作为头节点
					if (this.head == null) {
						this.head = newnode;

					} else {
						let current = this.head; //将链表的第一个节点赋值给current
						//判头节点的next是否为空,之后判断的都是当前节点的下一级节点(即当前节点的next)
						while (current.next != null) {
							//如果不为空,则将该节点的next作为新的节点继续进行判断
							current = current.next
						};

						current.next = newnode; //将新节点赋值给该节点的next
					}
					this.length++
				}

				//2、insert(指定位置插入新节点)
				insert(position, data) { //设置形参
					//判断位置参数的可行性
					if (position < 0 || position > this.length || !Number.isInteger(position)) {
						return false
					}
					//创建新节点
					let newnode = new Lnode(data)
					//判断首位插入的两种可能
					if (position == 0) {
						// 原链表中没有节点则直接让头节点指向新节点
						if (this.head == null) {
							this.head = newnode
							// 原链表有节点,则让新节点的next指向头节点,让头节点指向新节点
						} else {
							newnode.next = this.head
							this.head = newnode
						}
						// 判断插入的位置为链表最末尾时则直接调用append方法即可
					} else if (position == this.length) {
						this.append(data)
						//最后一种可能,则是在链表的中间某一位置插入
					} else {
						let current = this.head //将链表的第一个节点赋值给current
						let index = 0 //自增符
						while (index < position - 1) { //找到插入位置的前一个节点
							current = current.next //将该节点的next属性赋值给current(即该节点指向的下一个节点)
							index++
						}
						newnode.next = current.next //将新节点的next指向新节点的前一个节点的next
						current.next = newnode //再将新节点的前一个结点的next指向新节点
					}
					this.length++
				}

				// 3、移除指定位置的元素
				remove(position) {
					//判断位置参数的可行性
					if (position < 0 || position > this.length || !Number.isInteger(position)) {
						return false
					}
					// console.log(this.head,1)
					//判断链表是否为空
					if (this.head == null) {
						return
					} else {
						//删除的节点为第一个节点时
						if (position == 0) {
							//直接将头节点指向头节点的下一个节点
							this.head = this.head.next
						} else {
							let current = this.head,
								index = 0
							while (index < position - 1) {
								current = current.next
								index++
							}
							current.next = current.next.next
						}
						this.length--
					}
				}

				//indexof查询指定的的元素,若存在则返回当前位置即index,不存在则返回-1
				indexOf(ele) {
					let current = this.head
					let index = 0
					while (index < this.length) {
						if (current.data == ele) {

							return index
						} else {
							current = current.next
							index++
						}
					}
					return -1
				}
				consle
				// 6.将链表中的数据连接为字符串
				toString() {
					let current = this.head,
						index = 0,
						res = "";
					while (index < this.len) {
						res += "-" + current.data;
						current = current.next;
						index++;
					}
					return res.slice(1)
				}
			}

			let list = new LinkList();
			for (let i = 0; i < 5; i++) {
				list.append(i)
			}
			list.insert(0, 66)
			console.log(list);
			list.remove(1)
			console.log(list);
			console.log(list.indexOf(3))
		

​

打印结果为

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值