Skip to main content

Prefix to postfix conversion using stack in typescript

Ts program for Prefix to postfix conversion using stack. Here more information.

// TypeScript program for
// Prefix to postfix 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 prefix expression to 
	// postfix expression
	public prefixToPostfix(prefix: string)
	{
		// Get the size
		var size = prefix.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 = size - 1; i >= 0; i--)
		{
			// Check whether given prefix location
			// at [i] is an operator or not
			if (this.isOperator(prefix.charAt(i)))
			{
				// When operator exist
				// Check that two operands exist or not
				if (s.size() > 1)
				{
					op1 = s.pop();
					op2 = s.pop();
					auxiliary = op1 + op2 + prefix.charAt(i);
					s.push(auxiliary);
				}
				else
				{
					isValid = false;
				}
			}
			else if (this.isOperands(prefix.charAt(i)))
			{
				// When get valid operands
				auxiliary = "" + prefix.charAt(i);
				s.push(auxiliary);
			}
			else
			{
				// Invalid operands or operator
				isValid = false;
			}
		}
		if (isValid == false)
		{
			// When have something wrong
			console.log("Invalid Prefix : " + prefix);
		}
		else
		{
			// Display calculated result
			console.log(" Prefix : " + prefix);
			console.log(" Postfix  : " + s.pop());
		}
	}
	public static main(args: string[])
	{
		var task = new Conversion();
		var prefix = "+*ab^cd";
		task.prefixToPostfix(prefix);
		prefix = "-+*^%adcex*y^ab";
		task.prefixToPostfix(prefix);
	}
}
Conversion.main([]);
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */

Output

 Prefix : +*ab^cd
 Postfix  : ab*cd^+
 Prefix : -+*^%adcex*y^ab
 Postfix  : ad%c^e*x+yab^*-




Comment

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