链表::查询

本文介绍了一种使用JavaScript实现简单链表查询的方法。通过定义链表节点结构,并创建包含多个节点的链表,演示了如何查找具有特定ID的节点。

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
  <title>Untitled Document</title>
 </head>
 <body>
  <p>实现简单的链表查询</p>
  <script type="text/javascript">
   function list(nodeId,nodeName){
    this.id=nodeId;
    this.name=nodeName;
    this.next=null; 
   }
   function create_list(){
    var node1 = new list(1, 'bgg');
    var node2 = new list(2, 'aihui');
    var node3 = new list(3, 'bob');
    var ptr = head = node1;
    ptr.next = node2;
    ptr = ptr.next;
    ptr.next = node3;
    ptr = ptr.next;
    return head;
   }
   
   function find_list(head,id){
    var ptr=head;
    while(ptr){
     if(ptr.id==id){
      return ptr;
     }else{
      ptr=ptr.next;
     }
    }
    return ptr;
   }
   var head=create_list();   
   var second=find_list(head,2);
   alert(second.name);
   </script>
 </body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值