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.

Stack

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

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

    this.stackEmpty = function() {
        return (top === 0);
    };

    this.push = function(e) {
        elements[++top] = e;
    };

    this.pop = function() {
        return (this.stackEmpty()) ? null : elements[top--];
    };
}