数据结构之---单向链表(js)

链表的数据结构

在这里插入图片描述

简单链表的代码实现

<script>
        function LinkedList() {
            //创建新结点函数
            function Node(data) {
                this.data = data;
                this.next = null;
            }
            this.head = null;
            this.length = 0;
            // 1、append方法
            LinkedList.prototype.append = function(data) {
                    //1、创建新的结点
                    var newNode = new Node(data);
                    // 2、判断是不是第一个节点
                    if (this.length == 0) { //是第一个结点
                        this.head = newNode;
                    } else { //不是第一个结点
                        var current = this.head;
                        // 找到最后一个结点
                        while (current.next) {
                            current = current.next;
                        }
                        // 最后结点的next指向新的结点
                        current.next = newNode
                    }
                    // 长度+1
                    this.length += 1;
                }
                // 2、toString方法
            LinkedList.prototype.toString = function() {
                    var current = this.head;
                    var listString = "";
                    while (current) {
                        listString += current.data + " ";
                        current = current.next;
                    }
                    return listString;
                }
                // 3、get方法
            LinkedList.prototype.get = function(position) {
                    //越界判断
                    if (position < 0 || position >= this.length) return null;
                    var index = 0;
                    var current = this.head;
                    while (index++ < position) {
                        current = current.next;
                    }
                    return current.data;
                }
                // 4、insert方法
            LinkedList.prototype.insert = function(position, data) {
                    if (position < 0 || position > this.length) return false;

                    var newNode = new Node(data);
                    if (position == 0) {
                        newNode.next = this.head;
                        this.head = newNode;
                    } else {
                        var index = 0;
                        var percious = null;
                        var current = this.head;
                        while (index++ < position) {
                            percious = current;
                            current = current.next;
                        }
                        percious.next = newNode;
                        newNode.next = current;
                    }
                    this.length += 1;
                    return true;
                }
                // 5、updata方法
            LinkedList.prototype.update = function(position, newData) {
                    if (position < 0 || position >= this.length) return false;
                    // if (position == 0) {         
                    //     this.head.data = newData;  
                    // } else {
                    var current = this.head;
                    var index = 0;
                    while (index++ < position) {
                        current = current.next;
                    }
                    current.data = newData;
                    // }
                    return true;

                }
                // 6、indexOf方法 
            LinkedList.prototype.indexOf = function(data) {
                    var current = this.head;
                    var index = 0;
                    while (current) {
                        if (current.data == data) {
                            return index;
                        }
                        current = current.next;
                        index += 1;
                    }
                    return -1;
                }
                // 7、removeAt方法
            LinkedList.prototype.removeAt = function(position) {
                    if (position < 0 || position >= this.length) return null;
                    var current = this.head;
                    if (position == 0) {
                        this.head = this.head.next;
                    } else {
                        var index = 0;
                        var percious = null;
                        while (index++ < position) {
                            percious = current;
                            current = current.next;
                        }
                        percious.next = current.next;
                    }
                    this.length -= 1;
                    return current.data;
                }
                // 8、remove方法
            LinkedList.prototype.remove = function(olddata) {
                    var index = this.indexOf(olddata);
                    this.removeAt(index);
                }
                // 9、isEmpty方法
            LinkedList.prototype.isEmpty = function() {
                    return this.length == 0;
                }
                // 10、size方法
            LinkedList.prototype.size = function() {
                return this.length;
            }
        }
        var linked = new LinkedList();
        linked.append(1);
        linked.append(3);
        linked.append(2);
        var index = linked.toString();
        alert(index);
        // var get = linked.get(2);
        // alert(get);
        // var index = linked.indexOf(0);
        // alert(index);
        // linked.insert(3, "ab");
        // linked.update(2, "ab");
        // linked.removeAt(2);
        linked.remove(2);
        var index1 = linked.toString();
        alert(index1);
        var length = linked.size();
        alert(length);
    </script>

我个人认为链表只要搞清楚其指针的指向也就是next的指向,链表还是非常容易理解的,在插入和删除结点的时候,要特别注意结点的指向问题,删除结点时,当没有引用指向该要删除的结点时,结点就会自动被收回。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值