js链表linkedList类(链表)

 

1,链表

***创键链表:

function linkedlist(){           //创建数据结构,一个对象,以后用来创建数据实例
  var   Node=function(element){  //辅助类,表示要添加进列表的项

this.element=element;   // 要添加的数据

this.next=null;   //指向列表下一项的指针;

}

var length=0;   //存储列表的项的长度;

var  head=null;   //存储第一个节点的引用;

}

 

***从链表增加元素:

this.append=function(element){
var node=new Node(element){  //  创建node项,把值传入

current;  // 创键一个迭代指针,可以遍历链表的;

if(head===null)  //如果链表是空的

  head=node;       //则node是链表的第一个项,使第一个项指向node

else{    //如果链表本来就有值

while(current.next){     //一直往下迭代寻找最后一个项

current=current.next;

}

current.next=node; //找到了最后一个项目,本来它的next=null,现在使它的next=node;

}

length++; //更新链表长度;

}

 

***从链表移除制定位置(position=5)元素:

this.removeAt=function(position){}

a, 这个元素是链表的第一个元素:current指向链表中的元素,令current=head;  head=current.next;直接抛弃current;

b.这个元素是链表的最后一个或中间的元素:

var index=0;//创建一个累加;

while(index<pisition)  //position是要删除的位置,比如5;

{

previous=current;  //创建指向迭代指针的前一个位置

current=current.next; //迭代指针向下寻找

index++;  

}    //index++执行后,index=3,{}中内容不再执行,current指向要删除元素的位置,previous指向前一个位置,令previous的下一个元素是current的下一个元素,即删除了current。

previous.next=current.next;

***tostring()获取链表中值的字符串:

this.tostring=function(){
var current=head;  //让current指向链表头部

var string='  ';

while(current){

string+=current.element   //current指向链表,可以引用元素。之前有个,var Node=function(element){  this.element=element}, 这个this指向LinkedList()链表。因为current=head,所以current可以直接引用数据。

current=current.next;    

}

return string;

***从链表查询元素位置Indexof():
this.indexOf=function(element){

var current=head;

var index=-1;

while(current){
index++;

if(element==current.element){  
return index;  }

current=current.next;

}}

return -1;

}

***从链表删除指定元素(element=hello):

this.remove=function(element){

var position=this.indexof(element);

 return     this.removeAt(position);

}

***询问链表是否为空:

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

***询问链表长度:

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

}

***返回head头指针:

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


一个实例:创建一个链表,可以实现插入数据和(通过下标)寻找对应链表的值;

//创建链表对象linkedlist
var linkedlist=function(element,index){

   //创建结点对象Node
    var Node=function(element){
        this.element=element;
        this.next=null;
    }
    var head = null;
    var length = 0;

//向链表中插入值;
    this.append=function(element){
        var  node=new Node(element),current;
        if(head==null){
            head=node;
        }else{
            current = head;
            while(current.next){
                current = current.next;
            }
            current.next= node;
        }
        length++;
    }

//获取链表中的值
    this.get=function(index){
        var num=0,current;
        if(index<0){
            return 0;
        }else if(index==0){
            return head;
        }else{
            current = head;
            while(num++<index){
                current=current.next;
            }
          var  a = current.element;
            return a;
        }

    }
}
//运用链表
var  a=new linkedlist(); //实例化一个链表
a.append(1); //向链表中添加值
a.append(2);
a.append(3);
a.append(4);
var b=a.get(2);  //获取链表中的第三个值;
console.log(b);       //打印

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值