JavaScript -- 链表的JavaScript描述

链表是由一组节点组成的集合。每个节点都使用一个对象的引用指向它的后继。指向另一个节点的引用叫做链。

看下图:

header节点不存数据,我每一次就从头开始遍历节点,拿到头就拿到整条链表。

我们可以定义个数据结构用来表示每一个节点。

let Node = function (element) {
    this.element = element;//数据域
    this.next = null;//下一个节点
}

下面介绍增删查三种操作。

增操作:(规定我们每一次在链表尾部添加数据)

    this.insert = function (data) {
         let newNode = new Node(data);//新生成一个节点
         let cur = this.head ;
         while (cur.next != null) {//找链表尾
             cur = cur.next;
         };
         cur.next = newNode;//连起来
    }

删操作:(给定我们一个数据,如果数据在链表中出现,那么删除第一次出现的时候)

    this.remove = function (data) {
        let cur = this.head;
        while (cur.next != null) {
            let pre = cur;//因为需要删除cur.next节点,那么cur肯定需要保存,可以看图明白为啥
            cur = cur.next;
            if (cur.element == data) {
                pre.next = cur.next;//关键
                break;
            }
        };
    }

如果你需要删除所有的相关值,那么你只需要一个循环即可。

查操作:

    this.find = function (data) {
        let cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
            if (cur.element == data ) return true;
        }
        return false;
    }

结合删除操作可以删除所有相关值。

 

const Node = function (element) {
    this.element = element;
    this.next = null;
}
const List = function () {
    this.head = new Node("#head#");
    this.insert = function (data) {
         let newNode = new Node(data);
         let cur = this.head ;
         while (cur.next != null) {
             cur = cur.next;
         };
         cur.next = newNode;
    }
    this.remove = function (data) {
        let cur = this.head;
        while (cur.next != null) {
            let pre = cur;
            cur = cur.next;
            if (cur.element == data) {
                pre.next = cur.next;
                break;
            }
        };
    }
    this.find = function (data) {
        let cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
            if (cur.element == data ) return true;
        }
        return false;
    }
    this.reverse = function () {//反转
        let cur = this.head.next;
        let per = null;
        if (cur == null) return true;
        while (cur.next != null) {
            const p = cur;
            cur = cur.next;
            p.next = per;
            per = p;
        }
        cur.next = per;
        this.head.next = cur;
    }
    this.deleteBackNth = function (n) {//删除链表倒数第 n 个结点
        let fast = this.head;
        let slow = this.head;
        let per = null;
        for (let i = 0; i < n; i++) {
            if (fast.next != null) fast = fast.next;
            else return false;
        }
        while (fast.next != null) {
            per = slow;
            slow = slow.next;
            fast = fast.next;
        }
        if (per == null) {
            const first = this.head.next;
            this.head.next = first.next;
            first.next = null;
        } else {
            per = slow;
            slow = slow.next;
            per.next = slow.next;
            slow.next = null;
        }
    }
    this.isCircle = function () {//是否有环
        let fast = this.head;
        let slow = this.slow;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) return true;
        }
        return false;
    }
    this.show = function () {
        let cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
            console.log(cur.element);
        }
    }
    
}

看一下验证代码:

let app = new List();
 
app.insert(5);
app.insert(6);
app.insert(6);
app.insert(9);
app.insert(9);

app.remove(6);
 
app.show();

while (app.find(9)) {
    app.remove(9);
}
app.show();
 

5 6 9 9因为删除了一个6

5 6 因为删除了所有的9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值