JavaScript数据结构与算法(五):循环链表

function CircleLinkedList() {
    var Node = function (ele) {
        this.element = ele;
        this.next = null;
      }
    var length = 0;
    var head = null;
    var tail = null;
    // 向后添加元素
    this.append = function (ele) {
        var new_node = new Node(ele);
        var current = head;
        if(!head) {
            head = new_node;
            tail = new_node;
            tail.next = head;
        } else {
            while(current.next) {
                current = current.next;
            }
            current.next = new_node;
            tail = new_node;
            tail.next = head;
        }
        length++;
    }
    // 选择插入
    this.insert = function (pos, ele) { 
        // 检查pos是否越界
        if(pos >= 0 && pos <= length) {
            var index = 0, current = head, previous;
            var new_node = new Node(ele);
            // 插入在首结点
            if(pos === 0) {
                var tmp = head.next;
                head = new_node;
                head.next = tmp;
                tail.next = head;
            } else if(pos == length-1) {
                current = tail;
                current.next = new_node;
                tail = new_node;
                tail.next = head;
            } else {
                while(index < pos) {
                    previous = current;
                    current = current.next;
                    index++;
                }
                new_node.next = current;
                previous.next = new_node;
            }
            length++;
            return ele;
        } else {
            return null;
        }
    }
    /**
     * @param {需要删除的元素位置} ele 
     * @return 返回被删除元素
     */ 
    this.removeAt = function (pos) {  
        if(pos > -1 && pos < length) {
            var previous, current = head;
            var index = 0;
            // 删除首结点
            if(pos === 0) {
                head = current.next;
                tail.next = head;
            } else if(pos == length-1) { //删除尾结点
                current = tail;
                previous = tail.prev;
                tail = previous;
                tail.next = head;
            } else {
                while(index < pos) {
                    previous = current;
                    current = current.next;
                    index++;
                }
                previous.next = current.next;
            }
            length--;
            return current.element;
        } else {
            return null;
        }
    }
    /**
     * @param {需要删除的元素} ele 
     * @return 返回被删除元素的下标,默认-1
     */ 
    this.remove = function(ele){
        var index = this.indexOf(ele);
        return this.removeAt(index);
    }
    this.indexOf = function (ele) {  
        var current = head, index = 0;
        while(current) {
            if(current.element == ele) {
                return index;
            }
            index++;
            current = current.next;
        }
        return -1;
    }
    this.isEmpty = function () {  
        return length == 0;
    }
    this.size = function () {  
        return length;
    }
    this.toString = function () {  
        var current = head;
        var retStr = "";
        while(current) {
            retStr += current.element;
            current = current.next;
            if(current) {
                retStr += ","
            }
        }
        return retStr;
    }
    this.print = function () {  
        var current = head, index = 0;
        while(index < length) {
            console.log(current.element);
            current = current.next;
            index++;
        }
    }
  }

  module.exports = CircleLinkedList;

猜你喜欢

转载自blog.csdn.net/w_bu_neng_ku/article/details/80623246