Skip to content

Instantly share code, notes, and snippets.

View gribnoysup's full-sized avatar

Sergey Petushkov gribnoysup

View GitHub Profile
@gribnoysup
gribnoysup / LinkedList.js
Last active August 28, 2015 13:59
Linked List implementation
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);
@gribnoysup
gribnoysup / StaticQueue.js
Last active August 28, 2015 08:55
Static Queue implementation
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;
}
}
@gribnoysup
gribnoysup / Stack.js
Last active August 28, 2015 08:04
Super simple stack implementation for data structure tests
function Stack () {
this.__arr = [];
}
Stack.prototype = {
constructor : Stack,
push : function push (elem) {
if (typeof elem !== 'undefined') return this.__arr.push(elem);
},
pop : function pop () {
@gribnoysup
gribnoysup / infixToPostfixWithParentheses.js
Created August 27, 2015 17:25
Infix to postfix convertion (with parenthesis)
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()) {
@gribnoysup
gribnoysup / infixToPostfixNoParentheses.js
Created August 27, 2015 17:25
Infix to postfix convertion (without parentheses)
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 {
@gribnoysup
gribnoysup / evalPostfix.js
Created August 27, 2015 17:23
Evaluate postfix expression
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();
@gribnoysup
gribnoysup / balancedParentheses.js
Last active August 27, 2015 17:22
Balanced Parenthesis check
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]) {