JavaScript数据结构与算法Item4--链表_数据结构中链表item算法

return head;

};


总的结构代码:



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

  this.head = null;
  this.length = 0;

  this.append = function(element){
    var node = new Node(element),
        current;

    if(this.head == null){
      this.head = node;
    }else{
      current = this.head;
      while(current.next){
        current = current.next;
      }
    current.next = node;
    }
    this.length ++;
  }

  this.removeAt = function(position){
    var current = this.head,
    index = 0,
        prev;
    if(position > -1 && position < this.length){ //检查越界值
      if (position == 0){  //删除第一项,将head指向第一个元素即可;
        this.head = current.next;
      }
      else{
        while(index++ < position){ //将previous与current的下一项链接起来:跳过current,从而移除它,此处找到前一项;
          prev = current;
          current = current.next;
        }
        prev.next = current.next; //此处跳过current;
      }
      this.length --;
      return true;
    }else{
      return null;
    }
  }

  this.insert = function(position, element){
    var current = this.head,
    index = 0,
    prev,
    node = new Node(element);
    if(position> -1 && position < this.length){
      if(position == 0){
        this.head = node;
        node.next = current;
      }else{
        while(index++ < position){
          prev = current;
          current = current.next;
        }
        prev.next = node;
        node.next = current
      }
      this.length ++;
    }else{
      return false;
    }
  }

  this.indexOf = function(element){
    var current = this.head;
    index = -1;
    while(current){
      index ++;
      if(current.element === element){
        return index;
      }
      current = current.next;
    }
      return -1;
  }

  this.remove = function(element){
    var index = this.indexOf(element);
    this.removeAt(index);
  }

  this.isEmpty = function(){
    return this.length === 0;
  }

  this.size = function(){
    return this.length;
  }

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

  this.toString = function(){
    var current = this.head,
        string = '';
        while(current){
          string += current.element + '-->';
          current = current.next;
        }
        return string;
  }

}

var linkedList = new LinkedList();
linkedList.append(10);
linkedList.append(15);
linkedList.append(20);
linkedList.append(25);
linkedList.append(30);
linkedList.append(35);
console.log(linkedList.toString());
linkedList.removeAt(2);
console.log(linkedList.toString());
linkedList.insert(2,111);
console.log(linkedList.toString());
console.log(linkedList.indexOf(10))


### 2 双向链表


链表有多种不同的类型,这一节介绍双向链表。双向链表和普通链表的区别在于,在链表中,   
 一个节点只有链向下一个节点的链接,而在双向链表中,链接是双向的:一个链向下一个元素,   
 另一个链向前一个元素,如下图所示:


![](http://i.imgur.com/Pwwfs3T.png)


先从实现DoublyLinkedList类所需的变动开始:



function DoublyLinkedList() {
var Node = function(element){
this.element = element;
this.next = null;
this.prev = null; //新增的

};
var length = 0;
var head = null;
var tail = null; //新增的
//这里是方法

}


#### **在任意位置插入一个新元素**


向双向链表中插入一个新项跟(单向)链表非常类似。区别在于,链表只要控制一个next   
 指针,而双向链表则要同时控制next和prev(previous,前一个)这两个指针。   
 ![](http://i.imgur.com/gLGi5Up.png)



this.insert = function(position, element){
var current = this.head,
index = 0,
prev,
node = new Node(element);
if(position> -1 && position < this.length){
if(position == 0){
if(!this.head){//插入空的链表中的首位
this.head = node;
this.tail = node;
}else{
this.head = node;
node.next = current;
current.prev = node;
}
}else if(position == this.length){
current = tail;
current.next = node;
node.prev = current;
this.tail = node;
}else{
while(index++ < position){
previous = current;
current = current.next;
}
previous.next = node;
node.next = current;
node.prev = previous;
current.prev = node;
}
this.length ++;
return true;
}else{
return false;
}
}


#### **从任意位置移除元素**


我们需要处理三种场景:从头部、从中间和从尾部移除一个元素。   
 ![](http://i.imgur.com/e1GVogB.png)



this.removeAt = function(position){
var current = this.head,
index = 0,
prev;
if(position > -1 && position < this.length){ //检查越界值
if (position == 0){ //删除第一项,将head指向第一个元素即可;
this.head = current.next;
if(this.length == 1){
this.tail = null;
}else{
this.head.prev = null;
}
}else if(position == this.length -1){
current = this.tail;
this.tail = current.prev;
this.tail.next = null;

     }
     else{
       while(index++ < position){ //将previous与current的下一项链接起来:跳过current,从而移除它,此处找到前一项;
         previous = current;
         current = current.next;
       }
       previous.next = current.next; //此处跳过current;
       current.next.prev = previous; 

     }
     this.length --;
     return current.element;
   }else{
     return null;
   }
 }

### 3 循环链表


循环链表可以像链表一样只有单向引用,也可以像双向链表一样有双向引用。循环链表和链   
 表之间唯一的区别在于,最后一个元素指向下一个元素的指针(tail.next)不是引用null,   
 而是指向第一个元素(head),如下图所示。


![](http://i.imgur.com/WyodomJ.png)   
 双向循环链表有指向head元素的tail.next,和指向tail元素的head.prev。   
 ![](http://i.imgur.com/AhA8THL.png)


双向链表总程序



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

 this.head = null;
 this.tail = null;
 this.length = 0;

 this.append = function(element){
   var node = new Node(element),
       current;

   if(this.head == null){
     this.head = node;
     this.tail = node;
   }else{
     current = this.head;
     while(current.next){
       current = current.next;
     }
   current.next = node;
   node.prev = current;
   this.tail = node;
   }
   this.length ++;
 }

 this.removeAt = function(position){
   var current = this.head,
   index = 0,
       prev;
   if(position > -1 && position < this.length){ //检查越界值
     if (position == 0){  //删除第一项,将head指向第一个元素即可;
       this.head = current.next;
       if(this.length == 1){
         this.tail = null;
       }else{
         this.head.prev = null;
       }
     }else if(position == this.length -1){
       current = this.tail;
       this.tail = current.prev;
       this.tail.next = null;

     }
     else{
       while(index++ < position){ //将previous与current的下一项链接起来:跳过current,从而移除它,此处找到前一项;
         previous = current;
         current = current.next;
       }
       previous.next = current.next; //此处跳过current;
       current.next.prev = previous; 

     }
     this.length --;
     return current.element;
   }else{
     return null;
   }
 }

 this.insert = function(position, element){
   var current = this.head,
   index = 0,
   prev,
   node = new Node(element);
   if(position> -1 && position < this.length){
     if(position == 0){
       if(!this.head){//插入空的链表中的首位
         this.head = node;
         this.tail = node;
       }else{
         this.head = node;
         node.next = current;
         current.prev = node;
       }
     }else if(position == this.length){
       current = tail;
       current.next = node;
       node.prev = current;
       this.tail = node;
     }else{
       while(index++ < position){
         previous = current;
         current = current.next;
       }
       previous.next = node;
       node.next = current;
       node.prev = previous;
       current.prev = node;
     }
     this.length ++;
     return true;
   }else{
     return false;
   }
 }

 this.indexOf = function(element){
   var current = this.head;
   index = -1;
   while(current){
     index ++;
     if(current.element === element){
       return index;
     }
     current = current.next;
   }
     return -1;
 }

 this.remove = function(element){
   var index = this.indexOf(element);
   this.removeAt(index);
 }

 this.isEmpty = function(){
   return this.length === 0;
 }
  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值