JavaScript实现链表

这篇博客介绍了链表数据结构的实现,包括创建链表、在头部和尾部添加元素、根据索引添加元素以及按索引删除元素的方法。代码示例详细展示了如何在JavaScript中操作链表。

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

链表是一种线性结构,通过引用字段将分离的元素连接在一起。

链表的实现

let MyList = function(){
            this.head = undefined;
            this.size = 0;
        }
        
        MyList.prototype.addHead = function(val){
            if(!this.head) this.head = {val:val,next:null};
            else{
                let node = {val:val,next:null};
                node.next = this.head;
                this.head = node;
            }
            this.size ++;
        }
        
        MyList.prototype.getByIndex = function(index){
            if(this.size == 0 || index >= this.size) return -1;
            let pre = this.head;
            let count = 0;
            while(pre){
                if(count == index) return pre.val;
                pre = pre.next;
                count ++;
            }
        }
        
        MyList.prototype.addTail = function(val){
            if(this.size == 0) this.head = {val,next:null};
            else{
                let pre = this.head;
                while(pre.next){
                    pre = pre.next;
                }
                pre.next = {val,next:null};
            }
            this.size++;
        }
        
        MyList.prototype.addAtIndex = function(index,val){
            if(index == 0){
                this.addHead(val);
            }else if(index == this.size){
                this.addTail(val);
            }else if(index < 0) return;
            else{
                let pre = this.head;
                let count = 1;
                while(pre){
                    if(index == count){
                        let node = {val,next:null};
                        node.next = pre.next;
                        pre.next = node;
                        this.size++;
                        return;
                    }
                    pre = pre.next;
                    count++;
                }
            }
        }
        
        MyList.prototype.deleteAtIndex = function(index){
            if(index <0 || index >= this.size) return;
            else if(index == 0){
                this.head = this.head.next;
                this.size --;
            }else if(this.size == index - 1){
                let pre = this.head;
                let count = 1;
                while(pre){
                    if(count == this.size - 1){
                        pre.next = null;
                        this.size --;
                        return;
                    }
                    pre = pre.next;
                    count ++;
                }
            }else{
                let pre = this.head;
                let count = 1;
                while(pre){
                    if(count == index - 1){
                        pre.next = pre.next.next;
                        this.size --;
                        return;
                    }
                    pre = pre.next;
                    count ++;
                }
            }
        }
        
        
        let list = new MyList();
        list.addHead("first");
        list.addHead("second");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值