链表(JavaScript)

 

 

 

1.链表结构实现: 

(1)append:尾部添加:分两种情况:一、链表为空,head设为node;二、链表不为空,获取到链表尾添加元素;

注意:链表添加后长度要手动增加

append代码实现:

var LinkList = function() {
    var head = null;
    var length = 0;

    // 辅助类:用于创建链表项
    function Node(ele) {
        this.ele = ele;
        this.next = null;
    }
    /**
     * append 尾部添加节点
     * 如果为空链表,则将创建的项赋值给head
     * 如果非空链表,则循环到最后,插入链表项
     * 添加元素时length++
    */
    this.append = function(ele) {
        var node = new Node(ele);
        if(head == null) {
            head= node;
        } else {
            var current = head;
            while(current.next) {
                current = current.next;
            }
            current.next = node;
        }
        length ++ ;
    }
    this.getHead = function() {
        return head;
    }
}    
var linkList = new LinkList();
linkList.append('小明');
linkList.append('小红');
linkList.append('小黄');
console.log(linkList.getHead());

 

(2)insert:链表中插入元素:分两种情况:

一、插入链表头;

二、向其他位置插入元素;

代码实现:

    // insert 链表中插入元素
    this.insert = function(position, ele) {
        var node = new Node(ele);
        if(position > -1 && position < length) {
            if(position == 0) {
                var current = head;
                head = node;
                node.next = current;
            } else {
                var previous = null;
                var current = head;
                var index = 0;
                while(index < position) {
                    previous = current;
                    current = current.next;
                    index ++ ;
                }
                previous.next = node;
                node.next = current;
            }
            length ++ ;
        }
    }

 

(3)removeAt:根据位置删除链表中元素:分两种情况:

一、删除链表头;

二、删除其他位置元素;

注意:删除后 length --,删除的数据一般会用到,所以最后返回删除的数据

代码实现:

    this.removeAt = function(position) {
        if(position > -1 && position < length) {
            if(position == 0) {
                var current = head;
                head = current.next;
            } else {
                var current = head;
                var previous = null;
                var index = 0;
                while(index < position) {
                    previous = current;
                    current = current.next;
                    index ++ ;
                }
                previous.next = current.next;
            }
            length -- ;
            return current;
        }
    }

 

(4)indexOf:获取某个元素的位置

代码实现:

    // indexOf 获取某项的位置
    this.indexOf = function(ele) {
        var current = head;
        var index = 0;
        while(current) {
            if(current.ele == ele) {
                return index;
            }
            current = current.next;
            index ++ ;
        }
        return -1;
    }

 

(5)remove :根据元素删除

    this.remove = function(ele) {
        return this.removeAt(this.indexOf(ele));
    }

(6)isEmpty:判断链表是否为空

        size:链表长度

    // isEmpty 是否为空
    this.isEmpty = function() {
        return length == 0;
    }
    // size 链表长度
    this.size = function() {
        return length;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值