数据结构与算法JavaScript描述读书笔记(js实现链表-单链表)

单链表

//创建构造函数创建节点
function Node(element){
    this.element = element;
    this.next = null;
}
//链表的构造函数
 function LList(){
    this.head = new Node('head');
    this.insert = insert;
    this.remove = remove;
    this.display = display;
    this.find = find;
    this.prefind = prefind;
 }
 function insert(newElement,item){
    var cur = this.find(item);
    var node = new Node(newElement);
    if(cur == null){
        alert('没有找到插入位置');
    }else{
        node.next = cur.next;
        cur.next = node;
    }
 }
 function find(item){
    var cur = this.head;
    while(cur.element != item){
        cur = cur.next;
    }
    return cur;
 }
 function display() {
     var cur = this.head;
     var str = '';
     while (cur.next != null){
        str += cur.next.element+'->';
        cur = cur.next;
     }
     return str;
 }
 function prefind(item){
    var cur = this.head;
    while(cur.next.element != item && cur.next != null){
        cur = cur.next;
    }
    return cur;
 }
 function remove(item){
    var pre = this.prefind(item);
    if(pre.next != null){
        pre.next = pre.next.next;
    }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值