This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Node (val) { | |
this.val = val; | |
this.next = null; | |
} | |
function LinkedList (firstVal) { | |
this.__head = null; | |
this.__length = 0; | |
if (typeof firstVal !== 'undefined') { | |
this.append(firstVal); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function StaticQueue (size) { | |
this.__arr = {}; | |
this.__size = size; | |
this.__front = -1; | |
this.__rear = -1; | |
for (var i = 0; i < size; i++) { | |
this.__arr[i] = undefined; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Stack () { | |
this.__arr = []; | |
} | |
Stack.prototype = { | |
constructor : Stack, | |
push : function push (elem) { | |
if (typeof elem !== 'undefined') return this.__arr.push(elem); | |
}, | |
pop : function pop () { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function infixToPostfixWithParentheses (expr) { | |
if (!balancedParentheses(expr)) return false; | |
var postfix = '', i = 0, len = 0; | |
var stack = new Stack(); | |
for (i, len = expr.length; i < len; i++) { | |
if (expr[i].match(/[^\(\)\+\-\*\/\%]/)) { | |
postfix += expr[i]; | |
} else if (stack.isEmpty()) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function infixToPostfixNoParentheses (expr) { | |
var postfix = '', i = 0, len = 0; | |
var stack = new Stack(); | |
for (i, len = expr.length; i < len; i++) { | |
if (expr[i].match(/[^\+\-\*\/\%]/)) { | |
postfix += expr[i]; | |
} else if (stack.isEmpty()) { | |
stack.push(expr[i]); | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function evalPostfix (expr) { | |
var i = 0, len = 0, op1, op2; | |
var stack = new Stack(); | |
for (i = 0, len = expr.length; i < len; i++) { | |
if (expr[i].match(/[^\+\-\*\/\%]/)) { | |
stack.push(expr[i]); | |
} else { | |
op2 = stack.pop(); | |
op1 = stack.pop(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function balancedParentheses (string) { | |
var stack = new Stack(); | |
var i, len; | |
string = string | |
// remove any strings or regexes used in target string (if it is a function for example) | |
.replace(/(\`.{0,}\`|\/.{0,}\/|\'.{0,}\'|\".{0,}\")/g, ''); | |
for (i = 0, len = string.length; i < len; i++) { | |
switch (string[i]) { |
NewerOlder