实现循环链表(js实现)

循环链表与单链表结构基本相似,唯一不同就是单链表的尾节点的next属性指向null,然而循环链表的尾节点的next属性指向首节点,这样就构成了一个循环

// 定义构造函数
function createNode(data,next) {
    var obj = {
        data: data,
        next: next
    };
    return obj;
}
// 定义链表函数
function Lists() {
    this.pRear = null;
    this.length = 0;
    this.InsertAtEnd = function (data) {
        if (this.length == 0) {
            var node = new createNode(data,this.pRear);
            this.pRear = node;
            node.next = this.pRear;
        } else {
            var node = new createNode(data,this.pRear.next);
            this.pRear.next = node;
            this.pRear = node;
        }
        this.length++;
    }
    this.InsertAtFirst = function (data) {
        if (this.length == 0) {
            var node = new createNode(data,this.pRear);
            this.pRear = node;
            node.next = this.pRear;
        } else {
            var node = new createNode(data,this.pRear.next);
            this.pRear.next = node;
        }
        this.length++;
    }
    this.Locate = function (index) {
        if (index < 0 || index > this.length - 1) {
            throw new Error("索引值有错");
        }   
        var temp = this.pRear.next;             
        for (var i = 0; i < index; i++) {
            temp = temp.next;
        }
        return temp;
    }
    this.Insert = function (index,data) {
        if (index < 0 || index > this.length) {
            throw new Error("索引值有错");
        }
        if (index == 0) {
            this.InsertAtFirst(data);
        } else if (index == this.length) {
            this.InsertAtEnd(data);
        } else {
            var node = new createNode(data,this.Locate(index));
            this.Locate(index - 1).next = node;
            this.length++;
        }
    }
    this.Search = function (data) {
        var temp = this.pRear.next;
        for (var i = 0; i < this.length; i++) {
            if (data == temp.data) {
                return i;
            }
            temp = temp.next;
        }
        return -1;
    }
    this.Clear = function () {
        this.length = 0;
        this.pRear = null;
    }
    this.Remove = function (index) {
        if (index < 0 || index > this.length - 1 || this.length == 0) {
            throw new Error("索引值有错");
        }
        if (this.length == 1) {
            this.Clear();
            return 1;
        }
        if (index == 0) {
            this.pRear.next = this.Locate(0).next;
        } else {
            this.Locate(index - 1).next = this.Locate(index).next;
        }
    }
    this.isEmpty = function () {
        return this.length == 0 ? true : false;
    }
    this.toString = function () {
        var temp = this.pRear.next,
            str = "";
        for (var i = 0; i < this.length; i++) {
            str += "第" + i + "个为:" + temp.data; 
            temp = temp.next; 
        }
        return str;
    }
}
// 测试
var list = new Lists();
list.Insert(0,1);
list.InsertAtFirst(0);
list.Insert(2,-1);
list.InsertAtEnd(20);
console.log(list.Search(2));
console.log(list.isEmpty());
console.log(list.toString());
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值