Linked List

Det er muligt at lave en linked list i JavaScript.
Du kan teste hvordan metoderne virker.


Linked List

: Data
: Index (Zero based index)





Selve kodenvar LinkedList = {
    root: null,

    //zero based index
    AddAfter: function(Data, index) {
        var count = 0;
        var tmp = this.root;
        while (tmp !== null) {
            if (count == index) {
                var node = { data: Data, next: tmp.next };
                tmp.next = node;
                break;
            }
            count++;
            tmp = tmp.next;
        }
    },

    //zero based index
    AddBefore: function(Data, index) {
        if (index == 0) {
            this.AddFirst(Data);
        }
        else {
            var count = 1;
            var tmp = this.root;
            while (tmp.next !== null) {
                if (count == index) {
                    var node = { data: Data, next: tmp.next };
                    tmp.next = node;
                    break;
                }
                count++;
                tmp = tmp.next;
            }
        }
    },

    AddFirst: function (Data) {
        var tmp = { data: Data, next: this.root };
        this.root = tmp;
    },

    AddLast: function(Data) {
        if (this.root !== null) {
            var tmp = this.root;
            while (tmp.next !== null) {
                tmp = tmp.next;
            }
            var node = { data: Data, next: null };
            tmp.next = node;
        }
        else {
            this.AddFirst(Data);
        }
    },

    Clear: function() {
        this.root = null;
    },

    //zero based index
    ChangeData: function(data, index) {
        var count = 0;
        var tmp = this.root;
        while (tmp !== null) {
            if (count == index) {
                tmp.data = data;
                break;
            }
            count++;
            tmp = tmp.next;
        }
    },

    Count: function() {
        var count = 0;
        var tmp = this.root;
        while (tmp !== null) {
            count++;
            tmp = tmp.next;
        }
        return count;
    },

    //zero based index
    GetData: function(index) {
        var data = null;
        var count = 0;
        var tmp = this.root;
        while (tmp !== null) {
            if (count == index) {
                data = tmp.data;
                break;
            }
            count++;
            tmp = tmp.next;
        }
        return data;
    },

    GetDataFirst: function() {
        if (this.root !== null) {
            return this.root.data;
        }
        else {
            return null;
        }
    },

    GetDataLast: function() {
        if (this.root !== null) {
            var tmp = this.root;
            while (tmp.next !== null) {
                tmp = tmp.next;
            }
            return tmp.data;
        }
        return null;
    },

    //zero based index
    Remove: function(index) {
        if (index == 0) {
            this.RemoveFirst();
        }
        else {
            var tmp = this.root;
            var count = 1;
            while (tmp.next !== null) {
                if (count == index) {
                    tmp.next = tmp.next.next;
                    break;
                }
                count++;
                tmp = tmp.next;
            }
        }
    },

    RemoveFirst: function() {
        if (this.root !== null) {
            this.root = this.root.next;
        }
    },

    RemoveLast: function() {
        if (this.root !== null) {
            var tmp = this.root;
            if (tmp.next !== null) {
                while (tmp.next.next !== null) {
                    tmp = tmp.next;
                }
                tmp.next = null;
            }
            else {
                this.root = null;
            }
        }
    },

    ToArray: function() {
        var arr = [];
        var index = 0;
        var tmp = this.root;
        while (tmp !== null) {
            arr[index] = tmp.data;
            index++;
            tmp = tmp.next;
        }
        return arr;
    }
};
Download koden som