数据结构与算法二(js 实现链表)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>链表的结构</title>
</head>
<body>
<script>

    function LinkedList(){
       function Node(data){
           this.data = data;
           this.next = null;
       }
        this.head = null;
        this.length = 0;
        //拼接
        LinkedList.prototype.append = function(data){
            var newNode = new Node(data);
            if(this.head == null){
                this.head = newNode;
            }else {
                var current = this.head;
                while (current.next)
                {
                    current = current.next;
                }
                current.next = newNode;
            }
            this.length +=1;
        }
        //插入
        LinkedList.prototype.insert = function (position,data) {
            //1.对position 进行越界判断
            //负数
            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 currentNode = this.head;

                var preNode;
                var index = 0;
                while (index < position){
                    preNode = currentNode;
                    currentNode = currentNode.next;
                    index +=1;
                }
                newNode.next = currentNode;
                preNode.next = newNode;
            }

        }
        //获取对应元素
        LinkedList.prototype.get = function (position) {
            if (position <0 || position >= this.length) return false;

            var currentNode = this.head;
            var index = 0;
            if(position>index++){
              currentNode = currentNode.next;
            }
            return currentNode.data;
        }
        LinkedList.prototype.indexOf = function (data) {
            var current = this.head;
            var index = 0;
            while (current){
                if(current.data == data){
                    return index;
                }else {
                    current = current.next;
                    index++;
                }
            }
            return -1;
        }
        //修改对应位置元素
        LinkedList.prototype.update = function (position,data) {
            if (position <0 || position >= this.length) return false;

            var current = this.head;
            var index = 0
            while (index ++ <position){
                current = current.next;
            }
            current.data = data;
            return true;
        }
        //删除对应位置元素
        LinkedList.prototype.removeAt = function (position) {
            if (position <0 || position >= this.length) return false;
            if (position == 0){
                this.head = this.head.next;
            }else {

                var current = this.head;
                var preNode =null;
                var index = 0;
                while (index++ <position){
                    preNode = current;
                    current = current.next;
                }
                preNode.next = current.next;
            }
            this.length -=1;
            return true;

        }
        //删除某个元素
        LinkedList.prototype.remove = function(data){
            var current = this.head;
            var position = this.indexOf(data);
            return this.removeAt(position);

        }
        //是否为空
        LinkedList.prototype.isEmpty = function () {
            return this.length == 0;
        }
        //有多少元素
        LinkedList.prototype.size = function () {
            return this.length;
        }
        //toString
        LinkedList.prototype.toString = function () {
            //1.定义变量
            var current = this.head;
            var listString = '';
            //2循环获取一个个节点
            while (current){
              listString += current.data +' ';
                current = current.next;
            }
            return listString;
        }
        this.length +=1;
        return true;
    }

    //测试代码
    var list = new LinkedList();
    list.append('abc')
    list.append('cba')
    list.append('nab')
    list.append('ai')
//    list.insert(4,'ca')
//    list.update(0,'aca')
    list.remove('ai');
    alert(list)
</script>


</body>
</html>

猜你喜欢

转载自blog.csdn.net/ZhaiAlan/article/details/92822046