【数据结构 JavaScript版】- web前端开发精品课程【红点工场】 --javascript-- 链表实现...

本文介绍了一种使用JavaScript实现链表数据结构的方法,通过定义节点和链表类,实现了链表的添加、插入、删除和查找等功能。适用于前端开发人员学习和理解链表的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>

<head>
    <title></title>
</head>

<body>
    <script>
    //【数据结构 JavaScript版】- web前端开发精品课程【红点工场】 --javascript-- 链表实现

    // 对象存储的方式


    var linkSheet = function() {
        var head = null;
        var length = 0;
        var Node = function(element) {
            this.element = element;
            this.next = null;
        }

        // 链表尾部添加元素
        this.append = function(element) {
            var node = new Node(element)
            if (head == null) {
                head = node;
            } else {
                var current = head;
                while (current.next) {
                    current = current.next;
                }
            }
            length++;
        }

        //链表插入元素
        this.insert = function(position, element) {
            if (position >= -1 && position < length) {
                var node = new Node(element);
                if (position == 0) {
                    var current = head;
                    head = node;
                    head.next = current;
                } else {
                    var index = 0;
                    var current = head;
                    var previous = null;
                    while (index < position) {
                        previous = current;
                        current = current.next;
                        index++;
                    }
                    previous.next = node;
                    node.next = current;
                }
                length++;
            }
        }


        // 移除下标元素
        this.removeAt = function(position){
        	if(position>-1&&position<length){
        		if(position==0){
        			var current = head;
        			head = current.next;
        		}
        		else{
        			var current = head;
        			var previous = null;
        			var index = 0;
        			while(index<position){
        				previous = current;
        				current = current.next;
        				index++;
        			}
        			previous.next= current.next;
        		}
        		length--;
        		return current;
        	}
        	return null;
        }

        // 获取元素下标
        this.indexOf = function(element){
        	var current = head;
        	var index = 0;
        	while(current){
        		if(current.element==element){
        			return index;
        		}
        		current= current.next;
        		index++;
        		return index;
        	}
        	return -1;
        }

       // 移除元素
       this.remove = function(element){
           return this.removeAt(this.indexOf(element));
       }

       // 判断为空
       this.isEmpty = function(){
       	return length==0;
       }

       // 获取长度
       this.size = function(){
       	return length;
       }


       // 返回头部

       this.getHead = function(){
       	return head;
       }


    }
    </script>
</body>

</html>

  

转载于:https://www.cnblogs.com/SunlikeLWL/p/10124318.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值