Skip to content

Instantly share code, notes, and snippets.

@wesleyhales
Created March 12, 2012 19:02
Show Gist options
  • Save wesleyhales/2023993 to your computer and use it in GitHub Desktop.
Save wesleyhales/2023993 to your computer and use it in GitHub Desktop.
JavaScript LinkedList Example
//I wanted a more elegant linkedlist example than the others, so purely for academic purposes I created one.
var LinkedList = function(e){
var that = {}, first, last;
that.push = function(value){
var node = new Node(value);
if(first == null){
first = last = node;
}else{
last.next = node;
last = node;
}
};
that.pop = function(){
var value = first;
first = first.next;
return value;
};
that.remove = function(index) {
var i = 0;
var current = first, previous;
if(index === 0){
//handle special case - first node
first = current.next;
}else{
while(i++ < index){
//set previous to first node
previous = current;
//set current to the next one
current = current.next
}
//skip to the next node
previous.next = current.next;
}
return current.value;
};
var Node = function(value){
this.value = value;
var next = {};
};
return that;
};
var linkedList = new LinkedList();
linkedList.push(1);
linkedList.push(2);
linkedList.push(3);
linkedList.push(4);
linkedList.remove(0);
console.log(linkedList.pop());
console.log(linkedList.pop());
console.log(linkedList.pop());
@drkibitz
Copy link

drkibitz commented Aug 9, 2013

Your that = {} and usage of new is confusing intention. Should probably be either that = this or just list = LinkedList();

@drkibitz
Copy link

drkibitz commented Aug 9, 2013

var next = {}; is unused.

@mrkrstrndd
Copy link

var list = function (e) {
var self = this;
var first, last, head;

    self.insert = function (value) {
        var node = new Node(value);
        if (first == null) {
            first = last = node;
        } else {
            var head = first;
            while (head.next != null) {
                head = head.next;
            }
            head.next = node;
            last = head.next;
        }
    }

    self.show = function () {
        var head = first;
        while (head != null) {
            console.log(head.value);
            head = head.next;
        }
    }

    self.remove = function (value) {
        var found = false;
        var head = first;
        while (head != null) {
            if (first.value == value) {
                prev = head = first = first.next;
                found = true;
            } else {
                if (head.value == value) {
                    found = true;
                    prev.next = head.next;
                }
                prev = head;
                head = head.next;                            
            }
        }

        if (!found) {
            console.log("#" + value + " not found");
        }
    }

    self.update = function (value, newValue) {
        var head = first;
        while (head != null) {
            if (head.value == value) {
                head.value = newValue;
            }
            head = head.next;
        }

    }

    var Node = function (value) {
        this.value = value;
        var next = {};
    }

    return self;
};

var list = new list();
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);

list.show();
console.log("____________________________");

list.update(2, 5);           
list.show();
console.log("____________________________");

list.remove(3);
list.show();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment