Posted on by Kalkicode
Code Stack

Postfix to prefix conversion using stack in node js

Js program for Postfix to prefix conversion using stack. Here problem description and other solutions.

// Node JS program for
// Postfix to prefix conversion

// Stack Node
class StackNode
{
	constructor(data, top)
	{
		this.data = data;
		this.next = top;
	}
}
class MyStack
{
	constructor()
	{
		this.top = null;
		this.count = 0;
	}
	// Returns the number of element in stack
	size()
	{
		return this.count;
	}
	isEmpty()
	{
		if (this.size() > 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	// Add a new element in stack
	push(data)
	{
		// 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
	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
	peek()
	{
		if (!this.isEmpty())
		{
			return this.top.data;
		}
		else
		{
			return "";
		}
	}
}
class Conversion
{
	// Check operator
	isOperator(text)
	{
		if (text == '+' || text == '-' || 
            text == '*' || text == '/' || 
            text == '^' || text == '%')
		{
			return true;
		}
		return false;
	}
	// Check operands
	isOperands(text)
	{
		if ((text >= '0' && text <= '9') || 
            (text >= 'a' && text <= 'z') || 
            (text >= 'A' && text <= 'Z'))
		{
			return true;
		}
		return false;
	}
	// Converting the given postfix expression to 
	// prefix expression
	postfixToPrefix(postfix)
	{
		// 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 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)
				{
					auxiliary = s.pop();
					auxiliary = s.pop() + auxiliary;
					auxiliary = postfix.charAt(i) + auxiliary;
					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(" Prefix  : " + s.pop());
		}
	}
}

function main()
{
	var task = new Conversion();
	var postfix = "ab+c*ef+g/+";
	task.postfixToPrefix(postfix);
	postfix = "abc*de-/+";
	task.postfixToPrefix(postfix);
}
// Start program execution
main();

Output

 Postfix : ab+c*ef+g/+
 Prefix  : +*+abc/+efg
 Postfix : abc*de-/+
 Prefix  : +a/*bc-de

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