javascript数据结构之链表

单链表

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

function LinkList() {
    this.root=null;
    this.find=find;
    this.insert=insert;
    this.display=display;
}

function find(element) {
    let node=this.root;
    while(node!=null){
        if(node.element==element)
            return node;
        else
            node=node.next;
    }
    return  null;
}

function insert(node) {
    if(this.root==null)
        this.root=node;
    else{
        let currrent=this.root;
        while (currrent.next!=null){
            currrent=currrent.next;
        }
        currrent.next=node;
    }
}


function display() {
    let node =this.root;
    while (node!=null){
        console.log(node.element);
        node=node.next;
    }
}


let node1=new Node(1);
let node2=new Node(2);
let node3=new Node(3);

let linklist=new LinkList();
linklist.insert(node1);
linklist.insert(node2);
linklist.insert(node3);
linklist.display();

双链表

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

function LinkList() {
    this.root=null;
    this.find=find;
    this.insert=insert;
    this.display=display;
}

function display() {
    let current=this.root;
    while (current!=null){
        console.log(current.element);
        current=current.next;
    }
}

function find(element) {
    let current=this.root;
    while (current!=null){
       if(current.element==element)
           return current;
       else
            current=current.next;
    }
    return null;
}

function insert(node) {
    if(this.root==null)
        this.root=node;
    else{
        let current=this.root;
        while (current.next!=null){
            current=current.next;
        }
        current.next=node;
        node.previous=current;

    }
}

let node1=new Node(3);
let node2=new Node(5);
let node3=new Node(4);

let linklist=new LinkList();
linklist.insert(node1);
linklist.insert(node2);
linklist.insert(node3);
linklist.display();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值