用js实现链表

版权声明:转载请注明出处,欢迎交流哦! https://blog.csdn.net/zdhui_fly/article/details/81612399

链表骨架

function LinkedList(){

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

      this.insert = function(position,element){}

      this.removeAt = function(position){}

      this.remove = function(element){}

      this.toString = function(){}

      this.isEmpty = function(){}

      this.size = function(){}

}

//实现链表

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

     
            //插入
            this.insert = function(position,element){
                if(position>=0&&position<=length){  //插入位置必须合理
                    var node = new Node(element);
                    var current = head,index = 0;
                    var previous;
                    if(position===0){               //在头部插入
                        node.next = current;
                        head = node;
                        tail = head;                //这是头、尾同一指向
                    }
                    else if(position===length){    //在尾部插入
                        tail.next = node;
                        tail = node;     
                    }
                    else{                           //在中间插入
                        while(index++<position){
                            previous = current;
                            current = current.next;
                        }
                        node.next = current;
                        previous.next = node;
                    }
                    length++;
                    return true;
                }
                else{
                    return false;
                }
            }
            //移除某个位置
            this.removeAt = function(position){
                if(position>=0&&position<=length){  //位置必须合理
                    var current = head,index = 0;
                    var previous;    
                    if(position===0){
                        head = current.next;
                    }       
                    else{
                        while(index++<position){
                            previous = current;
                            current = current.next;
                        }
                        previous.next = current.next;
                        
                    }           
                    length--;
                    return true;
                }
                else{
                    return false;
                }
            }
            //移除某个元素
            this.remove = function(element){
                var position = this.indexOf(element);
                return this.removeAt(position);
            }
            //indexOf方法
            this.indexOf = function(element){
                var current = head;
                var index = 0;
                while(current){
                    if(current.element===element){
                        return index;
                    }
                    else{
                        current=current.next;
                        index++;
                    }
                }
                return -1
            }
            //将链表转化成字符串
            this.toString = function(){              
                var str = "";
                var current = head;
                while(current){
                    str+=current.element+" ";
                    current=current.next;
                }
                return str;
            }
            this.size = function(){
                return length;
            }
            this.isEmpty = function(){
                return length===0;
            }
            this.getHead = function(){
                return head;
            }
        }

  //执行一下


        var list = new LinkedList()
        list.insert(0,0);list.insert(1,1);list.insert(1,2);list.insert(2,2);list.insert(2,3)
        list.toString();                   //0 2 3 2 1
        list.removeAt(1);list.toString();  //0 3 2 1
        list.remove(1); list.toString();   //0 3 2
        list.size();                       //3
        list.isEmpty()                     //false
        list.getHead();/*{element:0,next:{element:3,next:{element:2,next:null}}}*/

猜你喜欢

转载自blog.csdn.net/zdhui_fly/article/details/81612399
今日推荐