Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save alexhawkins/f98b00a4937f94053940 to your computer and use it in GitHub Desktop.

Select an option

Save alexhawkins/f98b00a4937f94053940 to your computer and use it in GitHub Desktop.
Functional, Functional-Shared, Prototypal, Pseudoclassical implementations of Stacks and Queues in JavaScript
/******************************************************/
/******************************************************/
/**
* FUNCTIONAL QUEUE
*/
/** Queues are FIFO, first in first out */
'use strict';
var makeQueue = function() {
//GLOBALS
/** @type {Object} our storage object for our queue */
var storage = {};
var length = 0; //keep track of queue length
/** @type {Object} basic queue */
var queue = {
enqueue: function(value) {
storage[length] = value;
length++;
return this;
},
dequeue: function() {
var fifo = storage[0];
if (length > 0) {
for (var i = 0; i < length; i++)
storage[i] = storage[i + 1];
length--;
}
return fifo;
},
size: function() {
return length;
},
viewQueue: function() {
return storage;
}
};
return queue;
}
var queue = makeQueue();
queue.enqueue('a');
queue.enqueue('b');
console.log(queue.dequeue());
console.log(queue.viewQueue());
queue.enqueue('c');
console.log(queue.viewQueue());
console.log(queue.dequeue());
/**
* FUNCTIONAL STACK
*/
/** Stacks are LIFO, last in first out */
var makeStack = function() {
var storage = {};
var length = 0;
var stack = {
push: function(value) {
storage[length] = value;
length++;
},
pop: function() {
/** get last value in storage */
if (length > 0) {
var result = storage[length - 1];
delete storage[length - 1];
length--;
}
return result;
},
size: function() {
return length;
},
view: function() {
return storage;
}
};
return stack;
};
var stack = makeStack();
stack.push('a');
stack.push('b');
stack.push('c');
console.log(stack.view());
stack.pop();
stack.pop();
stack.pop();
console.log(stack.pop());
/******************************************************/
/******************************************************/
/**
* FUNCTIONAL-SHARED QUEUE
*/
var makeQueue = function() {
var queue = {
storage: {},
_size: 0
};
extend(queue, queueMethods);
return queue;
};
function extend(obj1, obj2) {
for (var key in obj2)
obj1[key] = obj2[key];
}
var queueMethods = {
enqueue: function(value) {
this.storage[this._size] = value;
this._size++;
},
dequeue: function() {
var result = this.storage[0];
if (this._size > 0) {
for (var i = 0; i < this._size; i++)
this.storage[i] = this.storage[i + 1];
this._size--;
}
return result;
},
size: function() {
return this._size;
},
view: function() {
return this.storage;
}
};
var queue = makeQueue();
for (var i = 0, rnd; i < 100; i++) {
rnd = Math.floor((Math.random() * (123 - 97)) + 97);
queue.enqueue(String.fromCharCode(rnd));
}
console.log(queue.view());
console.log('D: ' + queue.dequeue());
console.log('D: ' + queue.dequeue());
console.log(queue.view());
/**
* FUNCTIONAL-SHARED STACK
*/
var makeStack = function() {
//intialize stack object
var stack = {
storage: {},
_size: 0
};
//extend stack object
extend(stack, stackMethods);
return stack;
};
var stackMethods = {
push: function(value) {
this.storage[this._size] = value;
this._size++;
},
pop: function() {
var result = this.storage[this._size - 1];
if (this._size > 0) {
delete this.storage[this._size - 1];
this._size--;
}
return result;
},
size: function() {
return this._size;
},
view: function() {
return this.storage;
}
};
var stack = makeStack();
console.log(stack);
stack.push(10);
stack.push(10);
stack.push(10);
stack.push(10);
console.log(stack.view());
stack.pop();
stack.pop();
console.log(stack.view());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment