Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Doubly Linked List - Javascript Code Bank


Doubly Linked List
A doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list.
                  var DoublyLinkedList = (function () {
                var Node = function (element) {
                    this.element = element;
                    this.next = null;
                    this.previous = null
                };
                var DoubleList = function () {
                    this.head = new Node('head');
                    this.length = 0
                };
                DoubleList.prototype.add = function (newElement) {
                    var newNode = new Node(newElement);
                    var current = this.findLast();
                    newNode.next = current.next;
                    newNode.previous = current;
                    current.next = newNode;
                    this.length++
                };
                DoubleList.prototype.addTo = function (newElement, id) {
                    var newNode = new Node(newElement);
                    var current = this.get(id);
                    newNode.next = current.next;
                    newNode.previous = current;
                    current.next = newNode;
                    this.length++
                };
                DoubleList.prototype.addFirst = function (newElement) {
                    var newNode = new Node(newElement);
                    var current = this.head;
                    newNode.next = current;
                    newNode.previous = current.previous;
                    this.head = newNode;
                    current.previous = newNode;
                    this.length++
                };
                DoubleList.prototype.clear = function () {
                    this.head.next = null;
                    this.length = 0
                };
                DoubleList.prototype.contains = function (item) {
                    var currNode = this.head;
                    while (currNode.element != item && currNode.next != null) {
                        currNode = currNode.next
                    }
                    if (currNode.element != item) {
                        return false
                    } else {
                        return true
                    }
                };
                DoubleList.prototype.find = function (item) {
                    var currNode = this.head;
                    while (currNode.element != item) {
                        currNode = currNode.next
                    }
                    return currNode
                };
                DoubleList.prototype.findLast = function () {
                    var currNode = this.head;
                    while (!(currNode.next == null)) {
                        currNode = currNode.next
                    }
                    return currNode
                };
                DoubleList.prototype.get = function (index) {
                    var currNode = this.head;
                    var counter = 0;
                    if (index > 0 && index <= this.length) {
                        while (!(currNode.next == null) && counter++ != index) {
                            currNode = currNode.next
                        }
                    }
                    return currNode
                };
                DoubleList.prototype.indexOf = function (item) {
                    var currNode = this.head;
                    var index = 0;
                    while (currNode.element != item) {
                        currNode = currNode.next;
                        index++
                    }
                    if (currNode != this.head && index == 0) {
                        index = -1
                    }
                    return index
                };
                DoubleList.prototype.remove = function (item) {
                    var currNode = this.find(item);
                    if (!(currNode.next == null)) {
                        currNode.previous.next = currNode.next;
                        currNode.next.previous = currNode.previous;
                        currNode.next = null;
                        currNode.previous = null;
                        this.length--;
                        return true
                    }
                    return false
                };
                DoubleList.prototype.removeAt = function (index) {
                    return this.remove(get(index))
                };
                DoubleList.prototype.size = function () {
                    return this.length
                };
                DoubleList.prototype.toArray = function () {
                    var array = [];
                    currNode = this.head;
                    while (currNode.next != null) {
                        array.push(currNode.element);
                        currNode = currNode.next
                    }
                    array.push(currNode.element);
                    return array
                };
                DoubleList.prototype.toJSON = function () {
                    var array = this.toArray();
                    return JSON.stringify(array)
                };
            
Comments
Sorry but there are no comments to display