From 43fa2866aea0b7ba24b63826cea5102c1047c12f Mon Sep 17 00:00:00 2001 From: hari Date: 2022年10月15日 22:13:41 +0530 Subject: [PATCH 1/7] Added program for conversion of infix to postfix conversion using stack --- Data-Structures/Stack/ToPostfix.js | 86 ++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Data-Structures/Stack/ToPostfix.js diff --git a/Data-Structures/Stack/ToPostfix.js b/Data-Structures/Stack/ToPostfix.js new file mode 100644 index 0000000000..db3cd5d2b1 --- /dev/null +++ b/Data-Structures/Stack/ToPostfix.js @@ -0,0 +1,86 @@ +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 += 1 + } + + // Returns and removes the last element of the Stack + pop () { + if (this.top !== -1) { + this.top -= 1 + 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.stack.length] + } + 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 = ""; + let opStack = new Stack(); + for (let c of infix) { + if (isAlNum(c)) + postfix += c; + else if(c == '(') + opStack.push(c); + else if(c == ')') { + while((x = opStack.pop()) != '(') + postfix += x; + } + else { + console.log("exec"); + console.log((opStack.stack), c) + while(priority(opStack.stack)> priority(c)) { + postfix += opStack.pop(); + console.log(postfix) + console.log("exec"); + } + + opStack.push(c) + } + } + while (opStack.top != -1) { + postfix += opStack.pop(); + } + + return postfix; +} + +export {ToPostfix} \ No newline at end of file From ac839d962d83ad9e38f60649bea8da708cc42786 Mon Sep 17 00:00:00 2001 From: hari Date: 2022年10月15日 22:13:58 +0530 Subject: [PATCH 2/7] Added test for infix to postfix conversion --- Data-Structures/Stack/test/ToPostfix.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Data-Structures/Stack/test/ToPostfix.test.js diff --git a/Data-Structures/Stack/test/ToPostfix.test.js b/Data-Structures/Stack/test/ToPostfix.test.js new file mode 100644 index 0000000000..b6abf4ff6d --- /dev/null +++ b/Data-Structures/Stack/test/ToPostfix.test.js @@ -0,0 +1,12 @@ +import { ToPostfix } from "../ToPostfix"; + +describe('ToPostfix', () => { + it('Convert 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); + + }) +}) \ No newline at end of file From 4f50d07a9d043aa03015f06ac8318ca749b216ec Mon Sep 17 00:00:00 2001 From: hari Date: 2022年10月15日 22:17:52 +0530 Subject: [PATCH 3/7] Added wikipedia links --- Data-Structures/Stack/ToPostfix.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Data-Structures/Stack/ToPostfix.js b/Data-Structures/Stack/ToPostfix.js index db3cd5d2b1..7d63ca540d 100644 --- a/Data-Structures/Stack/ToPostfix.js +++ b/Data-Structures/Stack/ToPostfix.js @@ -1,3 +1,9 @@ +/* + * 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 = [] From 9c1b6866244ee051629ba546969fd84f779f899b Mon Sep 17 00:00:00 2001 From: hari Date: 2022年10月15日 22:27:04 +0530 Subject: [PATCH 4/7] Missing variable declaration corrected --- Data-Structures/Stack/ToPostfix.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Data-Structures/Stack/ToPostfix.js b/Data-Structures/Stack/ToPostfix.js index 7d63ca540d..4b06f1a697 100644 --- a/Data-Structures/Stack/ToPostfix.js +++ b/Data-Structures/Stack/ToPostfix.js @@ -67,6 +67,7 @@ function ToPostfix(infix) { else if(c == '(') opStack.push(c); else if(c == ')') { + let x = ''; while((x = opStack.pop()) != '(') postfix += x; } From 7c860d76eb5bbf76baa67cadd6b9bf275c8469eb Mon Sep 17 00:00:00 2001 From: hari Date: 2022年10月16日 22:11:55 +0530 Subject: [PATCH 5/7] code formatted properly --- Data-Structures/Stack/ToPostfix.js | 139 +++++++++---------- Data-Structures/Stack/test/ToPostfix.test.js | 18 ++- 2 files changed, 77 insertions(+), 80 deletions(-) diff --git a/Data-Structures/Stack/ToPostfix.js b/Data-Structures/Stack/ToPostfix.js index 4b06f1a697..2b917cac90 100644 --- a/Data-Structures/Stack/ToPostfix.js +++ b/Data-Structures/Stack/ToPostfix.js @@ -5,89 +5,88 @@ */ 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 += 1 - } - - // Returns and removes the last element of the Stack - pop () { - if (this.top !== -1) { - this.top -= 1 - 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 + 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() } - - // Returns the last element without removing it - last () { - if (this.top !== -1) { - return this.stack[this.stack.length] - } - return null + 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.stack.length] } + return null + } } const isAlNum = (c) => { - return c.match(/^[a-z0-9]+$/i) !== null; + return c.match(/^[a-z0-9]+$/i) !== null } const priority = (op) => { - if(op=='+'||op=='-') - return 1; - else if(op=='*'||op=='/') - return 2; - - return 0; + if (op === '+' || op === '-') { + return 1 + } + else if (op === '*' || op === '/'){ + return 2 + } + return 0 } function ToPostfix(infix) { - let postfix = ""; - let opStack = new Stack(); - for (let 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 { - console.log("exec"); - console.log((opStack.stack), c) - while(priority(opStack.stack)> priority(c)) { - postfix += opStack.pop(); - console.log(postfix) - console.log("exec"); - } - - opStack.push(c) - } + let postfix = '' + let opStack = new Stack() + for (const c of infix) { + if (isAlNum(c)) { + postfix += c } - while (opStack.top != -1) { - postfix += opStack.pop(); + else if (c === '('){ + opStack.push(c) } - - return postfix; + else if (c === ')') { + let x = '' + while ((x = opStack.pop()) != '('){ + postfix += x + } + } + else { + while (priority(opStack.stack)> priority(c)) { + postfix += opStack.pop() + } + + opStack.push(c) + } + } + while (opStack.top !== -1) { + postfix += opStack.pop() + } + return postfix } -export {ToPostfix} \ No newline at end of file +export { ToPostfix } diff --git a/Data-Structures/Stack/test/ToPostfix.test.js b/Data-Structures/Stack/test/ToPostfix.test.js index b6abf4ff6d..8a27840de2 100644 --- a/Data-Structures/Stack/test/ToPostfix.test.js +++ b/Data-Structures/Stack/test/ToPostfix.test.js @@ -1,12 +1,10 @@ -import { ToPostfix } from "../ToPostfix"; +import { ToPostfix } from "../ToPostfix" describe('ToPostfix', () => { - it('Convert 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); - - }) -}) \ No newline at end of file + it('Convert 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) + }) +}) From e352aa09202b5619c2f863f97d7e74db6fc9cc6a Mon Sep 17 00:00:00 2001 From: hari Date: 2022年10月20日 20:09:01 +0530 Subject: [PATCH 6/7] Added test descriptions --- Data-Structures/Stack/ToPostfix.js | 28 ++++++++------------ Data-Structures/Stack/test/ToPostfix.test.js | 11 ++++++-- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Data-Structures/Stack/ToPostfix.js b/Data-Structures/Stack/ToPostfix.js index 2b917cac90..b9a0de28d1 100644 --- a/Data-Structures/Stack/ToPostfix.js +++ b/Data-Structures/Stack/ToPostfix.js @@ -49,38 +49,32 @@ const isAlNum = (c) => { } const priority = (op) => { + op = String(op) if (op === '+' || op === '-') { - return 1 - } - else if (op === '*' || op === '/'){ - return 2 + return 1 + } else if (op === '*' || op === '/') { + return 2 } return 0 } -function ToPostfix(infix) { - +function ToPostfix (infix) { let postfix = '' - let opStack = new Stack() + const opStack = new Stack() for (const c of infix) { if (isAlNum(c)) { postfix += c - } - else if (c === '('){ + } else if (c === '(') { opStack.push(c) - } - else if (c === ')') { + } else if (c === ')') { let x = '' - while ((x = opStack.pop()) != '('){ + while ((x = opStack.pop()) !== '(') { postfix += x } - } - else { + } else { while (priority(opStack.stack)> priority(c)) { postfix += opStack.pop() - } - - opStack.push(c) + } opStack.push(c) } } while (opStack.top !== -1) { diff --git a/Data-Structures/Stack/test/ToPostfix.test.js b/Data-Structures/Stack/test/ToPostfix.test.js index 8a27840de2..f8ffa5d215 100644 --- a/Data-Structures/Stack/test/ToPostfix.test.js +++ b/Data-Structures/Stack/test/ToPostfix.test.js @@ -1,10 +1,17 @@ -import { ToPostfix } from "../ToPostfix" +import { ToPostfix } from '../ToPostfix' describe('ToPostfix', () => { - it('Convert the given infix expression to postfix', () => { + 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) + }) }) From a6b0fe298715148e7bdc7bd451b6aeffe5e8e7d7 Mon Sep 17 00:00:00 2001 From: hari Date: 2022年10月22日 15:42:59 +0530 Subject: [PATCH 7/7] last() method changed --- Data-Structures/Stack/ToPostfix.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Data-Structures/Stack/ToPostfix.js b/Data-Structures/Stack/ToPostfix.js index b9a0de28d1..7a0aca9294 100644 --- a/Data-Structures/Stack/ToPostfix.js +++ b/Data-Structures/Stack/ToPostfix.js @@ -38,7 +38,7 @@ class Stack { // Returns the last element without removing it last () { if (this.top !== -1) { - return this.stack[this.stack.length] + return this.stack[this.length()] } return null } @@ -49,7 +49,6 @@ const isAlNum = (c) => { } const priority = (op) => { - op = String(op) if (op === '+' || op === '-') { return 1 } else if (op === '*' || op === '/') { @@ -72,7 +71,7 @@ function ToPostfix (infix) { postfix += x } } else { - while (priority(opStack.stack)> priority(c)) { + while (priority(opStack.last())> priority(c)) { postfix += opStack.pop() } opStack.push(c) }

AltStyle によって変換されたページ (->オリジナル) /