Skip to content
85 changes: 85 additions & 0 deletions Data-Structures/Stack/ToPostfix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Author: Harinath-B (https://github.com/Harinath-B)
* Infix and POstfix notation explanation can be found in the following links:
* Wikipedia: https://en.wikipedia.org/wiki/Infix_notation / https://en.wikipedia.org/wiki/Reverse_Polish_notation
*/

class Stack {
constructor () {
this.stack = []
this.top = -1
}

// Adds a value to the end of the Stack
push (newValue) {
this.stack.push(newValue)
this.top++
}

// Returns and removes the last element of the Stack
pop () {
if (this.top !== -1) {
this.top--
return this.stack.pop()
}
throw new Error('Stack Underflow')
}

// Returns the number of elements in the Stack
length () {
return this.top
}

// Returns true if stack is empty, false otherwise
isEmpty () {
return this.top === -1
}

// Returns the last element without removing it
last () {
if (this.top !== -1) {
return this.stack[this.length()]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work with 0-indexing?

}
return null
}
}

const isAlNum = (c) => {
return c.match(/^[a-z0-9]+$/i) !== null
}

const priority = (op) => {
if (op === '+' || op === '-') {
return 1
} else if (op === '*' || op === '/') {
return 2
}
return 0
}

function ToPostfix (infix) {
let postfix = ''
const opStack = new Stack()
for (const c of infix) {
if (isAlNum(c)) {
postfix += c
} else if (c === '(') {
opStack.push(c)
} else if (c === ')') {
let x = ''
while ((x = opStack.pop()) !== '(') {
postfix += x
}
} else {
while (priority(opStack.last()) > priority(c)) {
postfix += opStack.pop()
} opStack.push(c)
}
}
while (opStack.top !== -1) {
postfix += opStack.pop()
}
return postfix
}

export { ToPostfix }
17 changes: 17 additions & 0 deletions Data-Structures/Stack/test/ToPostfix.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ToPostfix } from '../ToPostfix'

describe('ToPostfix', () => {
it('Converting the given infix expression to postfix', () => {
const infix = 'a*b+(c-d/e)'
const postfix = ToPostfix(infix)
const expected = 'ab*cde/-+'
expect(postfix).toEqual(expected)
})

it('Testing empty input', () => {
const infix = ''
const postfix = ToPostfix(infix)
const expected = ''
expect(postfix).toEqual(expected)
})
})