js实现在单向循环链表的head后面插入一个node

在单向循环链表的head后面插入一个node。注意这里的单向循环链表类是为了调试代码

这里主要就是一个Node类(函数)以及一个add函数

function Node(value) {  //定义节点类
    this.value = value;
    this.next = null;
}
/*单向循环链表的实现,包括插入、打印操作
* */
function LinkedNode() {
    this.Node = Node;//链表会有一个节点
    this.head = null; //如果使用let head = null,那么定义在LinkedNode中的函数引入外边的函数还需要传入head
    this.length = 0;

    //toString打印链表
    this.toString = function(){
        let result = "";  //保存结果
        if(this.head === null){ //空链表,什么都不做
            return "空链表";
        }else{
            let current = this.head;
            while(current.next !== this.head){  //寻找到尾节点,不对尾节点操作,这个语句
                result += current.value + ",";
                current = current.next;
            }
            //对最后一个节点进行操作,其实也可以修改上面循环的语句条件
            result += current.value;
        }
        return result;
    }

    //创建链表,在尾节点插入
    this.append = function (value) {
        let node = new Node(value);  //创建一个节点,不使用this原因是这个本身不是链表中节点,而是插进入的
        let current;
        if(this.head === null){ //空链表的时候。注意头节点指向第一个节点
            this.head = node;
            node.next = this.head;
        }else{  //不为空链表
            current = this.head;  //current为遍历指针
            while(current.next !== this.head){  //寻找尾节点
                current = current.next;
            }
            current.next = node;
            node.next = this.head;
        }
        this.length++;
    }

    //在任何位置插入一个节点
    this.insert = insert;//如果在这里还需要传入head,就说不通
}
function insert(position, value){
    if(position >= 0 && position <= this.length){  //第一个位置即position为0,就是在头部插入;
        // 因为是循环链表,所以第一个插入和最后一个插入时一个位置,所以position不必为this.length
        let node = new Node(value);
        let current = this.head;
        let pre = null;
        let count = 0;
        if(this.length === 0){  //空链表的时候
            this.head = node;
            node.next = this.head;
        }else{
            //上面必须判断空链表,否则current为null,current.next报错,null没有属性和方法也就没有next
            if(position === 0){ //在头部插入
                while(current.next !== this.head){//必须查找尾结点。如果不将尾结点的next指向node,该链表就不是循环链表啦
                    current = current.next;
                }
                pre = current;  //指向尾结点
                current = current.next;//指向第一个节点
                //插入节点
                this.head = node;
                this.head.next = current;
                pre.next = this.head;
            }else {
                while(count < position){//寻找到position所对应的节点,最后current指向下标为position所对应的的节点
                    pre = current;
                    current = current.next;
                    count++;
                }
                pre.next = node;
                node.next = current;
            }

        }
        this.length++;
        return true;
    }else {
        return false;
    }
}

//在单循环链表的head后面插入一个node。
/**在链表类外面实现add功能,接受参数头结点,值
 * @param {Node} head
 * @param {number||string||boolean||Object||Symbol} value
 * @return {Node}
 */
function add(head,value) {
    //在单循环链表的head后面插入一个node。
    let node = new Node(value);  //需要有一个node节点,也就上面的Node类
    if(head === null){
        head = node;
        head.next = node;
    }else{
        let current = head;
        let pre = current;
        current = current.next;
        pre.next = node;
        node.next = current;
    }
    return head;  //返回head节点;如果不返回,空链表的操作就不能得到正确的结果
}
let linked  = new LinkedNode();
//linked.insert(0, 1);
//linked.insert(0, 2);
console.log(linked.toString());
let head = linked.head;  //将linked的节点赋值给head
linked.head = add(head,3);  //对linked里面的节点head重新赋值。
console.log(linked.toString());

百里于2020年5月9日

参考:

如果有错,请您指出!如有侵权,请联系我删除!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值