js 链表结构

 function Node(element){
            this.val=element;
            this.next=null;
            this.prev=null;
        }

        function Links(){

            this.head=new Node("Head");
            this.lastNode=this.head;
        }
        Links.prototype={
            //插入链表 按位置插入 或者 插入到末尾
            insert:function(element,location){

                var newNode=new Node(element);

                this.lastNode.next=newNode;

                newNode.prev=this.lastNode;
                
                this.lastNode=newNode;

                return newNode;
            },
            //查找链表
            findNode:function(keyword){
                var node = this.head;

                if(!node.next){

                    console.log("链表为空")
                    return false;
                }

                var tempStr="";

                while(node.val!=keyword){
                    node=node.next;
                }

                return node;
            },
            //删除节点
            removeNode:function(keyword){
                var node = this.findNode(keyword);

                if(node.next!=undefined){
                    node.prev.next=node.next;
                    node.next.prev=node.prev;
                    
                }
                //删除最后一个元素更新
                if(this.lastNode==node){
                    node.prev.next=null;
                    this.lastNode=node.prev;
                };

                node.next=null;
                node.prev=null;
                node=null;

            },
            //打印链表
            print:function(){
                
                var node = this.head;

                if(!node.next){

                    console.log("链表为空")
                    return false;
                }
                
                var tempStr="";

                while(node!=undefined){

                    tempStr+=node.val+"-->";
                    
                    node=node.next;
                }

                console.log(tempStr+"null")

            },
            //反向输出
            reversePrint:function(){

                var node=this.lastNode;
                
                var tempStr="last-->";

                while(node!=undefined){

                    tempStr+=node.val+"-->";
                    
                    node=node.prev;
                }

                console.log(tempStr+"null")
            },
            //查找最后一个元素
            lastNode:function(){

                return this.lastNode;
            }
            

        }

        var list=new Links();

        list.insert("看下")
        list.insert("是不")
        list.insert("1")
        list.insert("2")
        
        list.removeNode("2");

        list.print();

        list.reversePrint();

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值