Postfix to infix conversion using stack in typescript
Ts program for Postfix to infix conversion using stack. Here problem description and other solutions.
// TypeScript program for
// Postfix To Infix Conversion
// Stack Node
class StackNode
{
public data: string;
public next: StackNode;
constructor(data: string, top: StackNode)
{
this.data = data;
this.next = top;
}
}
class MyStack
{
public top: StackNode;
public count: number;
constructor()
{
this.top = null;
this.count = 0;
}
// Returns the number of element in stack
public number size()
{
return this.count;
}
public boolean isEmpty()
{
if (this.size() > 0)
{
return false;
}
else
{
return true;
}
}
// Add a new element in stack
public push(data: string)
{
// Make a new stack node
// And set as top
this.top = new StackNode(data, this.top);
// Increase node value
this.count++;
}
// Add a top element in stack
public string pop()
{
var temp = "";
if (this.isEmpty() == false)
{
// Get remove top value
temp = this.top.data;
this.top = this.top.next;
// Reduce size
this.count--;
}
return temp;
}
// Used to get top element of stack
public string peek()
{
if (!this.isEmpty())
{
return this.top.data;
}
else
{
return "";
}
}
}
class Conversion
{
// Check operator
public boolean isOperator(text: string)
{
if (text == '+' || text == '-' ||
text == '*' || text == '/' ||
text == '^' || text == '%')
{
return true;
}
return false;
}
// Check operands
public boolean isOperands(text: string)
{
if ((text >= '0' && text <= '9') ||
(text >= 'a' && text <= 'z') ||
(text >= 'A' && text <= 'Z'))
{
return true;
}
return false;
}
// Converting the given postfix expression to
// infix expression
public postfixToInfix(postfix: string)
{
// Get the size
var size = postfix.length;
// Create stack object
var s = new MyStack();
// Some useful variables which is using
// of to storing current result
var auxiliary = "";
var op1 = "";
var op2 = "";
var isValid = true;
for (var i = 0; i < size && isValid; i++)
{
// Check whether given postfix location
// at [i] is an operator or not
if (this.isOperator(postfix.charAt(i)))
{
// When operator exist
// Check that two operands exist or not
if (s.size() > 1)
{
op1 = s.pop();
op2 = s.pop();
auxiliary = "(" + op2 + postfix.charAt(i) + op1 + ")";
s.push(auxiliary);
}
else
{
isValid = false;
}
}
else if (this.isOperands(postfix.charAt(i)))
{
// When get valid operands
auxiliary = "" + postfix.charAt(i);
s.push(auxiliary);
}
else
{
// Invalid operands or operator
isValid = false;
}
}
if (isValid == false)
{
// When have something wrong
console.log("Invalid postfix : " + postfix);
}
else
{
// Display calculated result
console.log(" Postfix : " + postfix);
console.log(" Infix : " + s.pop());
}
}
public static main(args: string[])
{
var task = new Conversion();
var postfix = "ab+c*ef+g/+";
task.postfixToInfix(postfix);
postfix = "abc*de-/+";
task.postfixToInfix(postfix);
}
}
Conversion.main([]);
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/
Output
Postfix : ab+c*ef+g/+
Infix : (((a+b)*c)+((e+f)/g))
Postfix : abc*de-/+
Infix : (a+((b*c)/(d-e)))
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment