Fork me on GitHub

Algorithms

This is a demonstration of selected algorithms from the book Introduction to Algorithms by Cormen et. al., implemented in JavaScript by Felix H. Dahlke.

Queue

A simple data structure that stores elements as on a physical queue: The first element inserted is the first to be extracted (FIFO).

Sorry, your browser doesn't support HTML5 Canvas.
function Queue() {
    var head = 0,
        tail = 0,
        elements = [];

    this.enqueue = function(e) {
        elements[tail] = e;
        if (tail === elements.length)
            tail = 1;
        else
            tail++;
    };

    this.dequeue = function() {
        var e = elements[head];
        if (head === elements.length)
            head = 1;
        else
            head++;
        return (typeof e === "undefined") ? null : e;
    };
}