Prefix to infix conversion

Here given code implementation process.

// Java program
// Prefix to infix conversion
//Stack Node
class Node
{
	public String data;
	public Node next;
	public Node(String data)
	{
		this.data = data;
		this.next = null;
	}
}
class MyStack
{
	public Node top;
	public MyStack()
	{
		this.top = null;
	}
	//Check two operands exist or not
	public boolean is_two_operands()
	{
		int counter = 0;
		Node temp = this.top;
		while (temp != null && counter < 2)
		{
			counter++;
			temp = temp.next;
		}
		return counter == 2;
	}
	//Add a new element in stack
	public void push(String data)
	{
		//Make a new stack node
		Node new_node = new Node(data);
		if (new_node != null)
		{
			new_node.next = top;
			top = new_node;
		}
		else
		{
			System.out.print("Memory overflow\n");
		}
	}
	//Add a top element in stack
	public String pop()
	{
		String temp = "";
		if (top != null)
		{
			temp = top.data;
			top = top.next;
		}
		return temp;
	}
	public boolean is_empty()
	{
		if (this.top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	public String peek()
	{
		if (top != null)
		{
			return top.data;
		}
		else
		{
			return "";
		}
	}
	//Converting the given prefix expression to infix expression
	public void infix_conversion(String prefix)
	{
		//Get the size
		int size = prefix.length();
		//Create stack object
		MyStack collection = new MyStack();
		//Some useful variables which is using of to storing current result
		String auxiliary = "";
		String op1 = "";
		String op2 = "";
		boolean is_valid = true;
		for (int i = size - 1; i >= 0; i--)
		{
			//Check whether given prefix location at [i] is an operator or not
			if (prefix.charAt(i) == '+' || prefix.charAt(i) == '-' || prefix.charAt(i) == '*' || prefix.charAt(i) == '/' || prefix.charAt(i) == '^' || prefix.charAt(i) == '%')
			{
				// When operator exist
				// Check that two operands exist or not
				if (collection.is_two_operands() == true)
				{
					op1 = collection.pop();
					op2 = collection.pop();
					auxiliary = "(" + op1 + prefix.charAt(i) + op2 + ")";
					collection.push(auxiliary);
				}
				else
				{
					is_valid = false;
				}
			}
			else if ((prefix.charAt(i) >= '0' && prefix.charAt(i) <= '9') || (prefix.charAt(i) >= 'a' && prefix.charAt(i) <= 'z') || (prefix.charAt(i) >= 'A' && prefix.charAt(i) <= 'Z'))
			{
				auxiliary = "" + prefix.charAt(i);
				collection.push(auxiliary);
			}
			else
			{
				is_valid = false;
			}
		}
		if (is_valid == false)
		{
			System.out.print("Invalid Prefix : " + prefix);
		}
		else
		{
			System.out.print("\n Prefix : " + prefix);
			System.out.print("\n Infix  : " + collection.pop());
		}
	}
	public static void main(String[] args)
	{
		MyStack obj = new MyStack();
		String prefix = "+*ab^cd";
		obj.infix_conversion(prefix);
		prefix = "-+*^%adcex*y^ab";
		obj.infix_conversion(prefix);
	}
}

Output

 Prefix : +*ab^cd
 Infix  : ((a*b)+(c^d))
 Prefix : -+*^%adcex*y^ab
 Infix  : (((((a%d)^c)*e)+x)-(y*(a^b)))
//Include header file
#include <iostream>

#include<string.h>

using namespace std;
// C++ program
// Prefix to infix conversion
//Stack Node
class Node
{
	public: string data;
	Node * next;
	Node(string data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class MyStack
{
	public: Node * top;
	MyStack()
	{
		this->top = NULL;
	}
	//Check two operands exist or not
	bool is_two_operands()
	{
		int counter = 0;
		Node * temp = this->top;
		while (temp != NULL && counter < 2)
		{
			counter++;
			temp = temp->next;
		}
		return counter == 2;
	}
	//Add a new element in stack
	void push(string data)
	{
		//Make a new stack node
		Node * new_node = new Node(data);
		if (new_node != NULL)
		{
			new_node->next = this->top;
			this->top = new_node;
		}
		else
		{
			cout << "Memory overflow\n";
		}
	}
	//Add a top element in stack
	string pop()
	{
		string temp = "";
		if (this->top != NULL)
		{
			temp = this->top->data;
			this->top = this->top->next;
		}
		return temp;
	}
	bool is_empty()
	{
		if (this->top != NULL)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	string peek()
	{
		if (this->top != NULL)
		{
			return this->top->data;
		}
		else
		{
			return "";
		}
	}
	//Converting the given prefix expression to infix expression
	void infix_conversion(string prefix)
	{
		//Get the size
		int size = prefix.size();
		//Create stack object
		MyStack collection = MyStack();
		//Some useful variables which is using of to storing current result
		string auxiliary = "";
		string op1 = "";
		string op2 = "";
		bool is_valid = true;
		for (int i = size - 1; i >= 0; i--)
		{
			//Check whether given prefix location at [i] is an operator or not
			if (prefix[i] == '+' || prefix[i] == '-' || prefix[i] == '*' || prefix[i] == '/' || prefix[i] == '^' || prefix[i] == '%')
			{
				// When operator exist
				// Check that two operands exist or not
				if (collection.is_two_operands() == true)
				{
					op1 = collection.pop();
					op2 = collection.pop();
					auxiliary = "(" + op1 + prefix[i] + op2 + ")";
					collection.push(auxiliary);
				}
				else
				{
					is_valid = false;
				}
			}
			else if ((prefix[i] >= '0' && prefix[i] <= '9') || (prefix[i] >= 'a' && prefix[i] <= 'z') || (prefix[i] >= 'A' && prefix[i] <= 'Z'))
			{
				auxiliary = prefix[i];
				collection.push(auxiliary);
			}
			else
			{
				is_valid = false;
			}
		}
		if (is_valid == false)
		{
			cout << "Invalid Prefix : " << prefix;
		}
		else
		{
			cout << "\n Prefix : " << prefix;
			cout << "\n Infix  : " << collection.pop();
		}
	}
};
int main()
{
	MyStack obj = MyStack();
	string prefix = "+*ab^cd";
	obj.infix_conversion(prefix);
	prefix = "-+*^%adcex*y^ab";
	obj.infix_conversion(prefix);
	return 0;
}

Output

 Prefix : +*ab^cd
 Infix  : ((a*b)+(c^d))
 Prefix : -+*^%adcex*y^ab
 Infix  : (((((a%d)^c)*e)+x)-(y*(a^b)))
//Include namespace system
using System;
// C# program
// Prefix to infix conversion
//Stack Node
class Node
{
	public String data;
	public Node next;
	public Node(String data)
	{
		this.data = data;
		this.next = null;
	}
}
class MyStack
{
	public Node top;
	public MyStack()
	{
		this.top = null;
	}
	//Check two operands exist or not
	public Boolean is_two_operands()
	{
		int counter = 0;
		Node temp = this.top;
		while (temp != null && counter < 2)
		{
			counter++;
			temp = temp.next;
		}
		return counter == 2;
	}
	//Add a new element in stack
	public void push(String data)
	{
		//Make a new stack node
		Node new_node = new Node(data);
		if (new_node != null)
		{
			new_node.next = top;
			top = new_node;
		}
		else
		{
			Console.Write("Memory overflow\n");
		}
	}
	//Add a top element in stack
	public String pop()
	{
		String temp = "";
		if (top != null)
		{
			temp = top.data;
			top = top.next;
		}
		return temp;
	}
	public Boolean is_empty()
	{
		if (this.top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	public String peek()
	{
		if (top != null)
		{
			return top.data;
		}
		else
		{
			return "";
		}
	}
	//Converting the given prefix expression to infix expression
	public void infix_conversion(String prefix)
	{
		//Get the size
		int size = prefix.Length;
		//Create stack object
		MyStack collection = new MyStack();
		//Some useful variables which is using of to storing current result
		String auxiliary = "";
		String op1 = "";
		String op2 = "";
		Boolean is_valid = true;
		for (int i = size - 1; i >= 0; i--)
		{
			//Check whether given prefix location at [i] is an operator or not
			if (prefix[i] == '+' || prefix[i] == '-' || prefix[i] == '*' || prefix[i] == '/' || prefix[i] == '^' || prefix[i] == '%')
			{
				// When operator exist
				// Check that two operands exist or not
				if (collection.is_two_operands() == true)
				{
					op1 = collection.pop();
					op2 = collection.pop();
					auxiliary = "(" + op1 + prefix[i] + op2 + ")";
					collection.push(auxiliary);
				}
				else
				{
					is_valid = false;
				}
			}
			else if ((prefix[i] >= '0' && prefix[i] <= '9') || (prefix[i] >= 'a' && prefix[i] <= 'z') || (prefix[i] >= 'A' && prefix[i] <= 'Z'))
			{
				auxiliary = "" + prefix[i];
				collection.push(auxiliary);
			}
			else
			{
				is_valid = false;
			}
		}
		if (is_valid == false)
		{
			Console.Write("Invalid Prefix : " + prefix);
		}
		else
		{
			Console.Write("\n Prefix : " + prefix);
			Console.Write("\n Infix  : " + collection.pop());
		}
	}
	public static void Main(String[] args)
	{
		MyStack obj = new MyStack();
		String prefix = "+*ab^cd";
		obj.infix_conversion(prefix);
		prefix = "-+*^%adcex*y^ab";
		obj.infix_conversion(prefix);
	}
}

Output

 Prefix : +*ab^cd
 Infix  : ((a*b)+(c^d))
 Prefix : -+*^%adcex*y^ab
 Infix  : (((((a%d)^c)*e)+x)-(y*(a^b)))
<?php
// Php program
// Prefix to infix conversion
//Stack Node
class Node
{
	public $data;
	public $next;

	function __construct($data)
	{
		$this->data = $data;
		$this->next = null;
	}
}
class MyStack
{
	public $top;

	function __construct()
	{
		$this->top = null;
	}
	//Check two operands exist or not
	public	function is_two_operands()
	{
		$counter = 0;
		$temp = $this->top;
		while ($temp != null && $counter < 2)
		{
			$counter++;
			$temp = $temp->next;
		}
		return $counter == 2;
	}
	//Add a new element in stack
	public	function push($data)
	{
		//Make a new stack node
		$new_node = new Node($data);
		if ($new_node != null)
		{
			$new_node->next = $this->top;
			$this->top = $new_node;
		}
		else
		{
			echo "Memory overflow\n";
		}
	}
	//Add a top element in stack
	public	function pop()
	{
		$temp = "";
		if ($this->top != null)
		{
			$temp = $this->top->data;
			$this->top = $this->top->next;
		}
		return $temp;
	}
	public	function is_empty()
	{
		if ($this->top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	public	function peek()
	{
		if ($this->top != null)
		{
			return $this->top->data;
		}
		else
		{
			return "";
		}
	}
	//Converting the given prefix expression to infix expression
	public	function infix_conversion($prefix)
	{
		//Get the size
		$size = strlen($prefix);
		//Create stack object
		$collection = new MyStack();
		//Some useful variables which is using of to storing current result
		$auxiliary = "";
		$op1 = "";
		$op2 = "";
		$is_valid = true;
		for ($i = $size - 1; $i >= 0; $i--)
		{
			//Check whether given prefix location at [i] is an operator or not
			if ($prefix[$i] == '+' || $prefix[$i] == '-' || $prefix[$i] == '*' || $prefix[$i] == '/' || $prefix[$i] == '^' || $prefix[$i] == '%')
			{
				// When operator exist
				// Check that two operands exist or not
				if ($collection->is_two_operands() == true)
				{
					$op1 = $collection->pop();
					$op2 = $collection->pop();
					$auxiliary = "(". $op1 . $prefix[$i] . $op2 .")";
					$collection->push($auxiliary);
				}
				else
				{
					$is_valid = false;
				}
			}
			else if ((ord($prefix[$i]) >= ord('0') && ord($prefix[$i]) <= ord('9')) || (ord($prefix[$i]) >= ord('a') && ord($prefix[$i]) <= ord('z')) || (ord($prefix[$i]) >= ord('A') && ord($prefix[$i]) <= ord('Z')))
			{
				$auxiliary =  $prefix[$i];
				$collection->push($auxiliary);
			}
			else
			{
				$is_valid = false;
			}
		}
		if ($is_valid == false)
		{
			echo "Invalid Prefix : ". $prefix;
		}
		else
		{
			echo "\n Prefix : ". $prefix;
			echo "\n Infix  : ". $collection->pop();
		}
	}
}

function main()
{
	$obj = new MyStack();
	$prefix = "+*ab^cd";
	$obj->infix_conversion($prefix);
	$prefix = "-+*^%adcex*y^ab";
	$obj->infix_conversion($prefix);
}
main();

Output

 Prefix : +*ab^cd
 Infix  : ((a*b)+(c^d))
 Prefix : -+*^%adcex*y^ab
 Infix  : (((((a%d)^c)*e)+x)-(y*(a^b)))
// Node Js program
// Prefix to infix conversion
//Stack Node
class Node
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class MyStack
{
	constructor()
	{
		this.top = null;
	}
	//Check two operands exist or not
	is_two_operands()
	{
		var counter = 0;
		var temp = this.top;
		while (temp != null && counter < 2)
		{
			counter++;
			temp = temp.next;
		}
		return counter == 2;
	}
	//Add a new element in stack
	push(data)
	{
		//Make a new stack node
		var new_node = new Node(data);
		if (new_node != null)
		{
			new_node.next = this.top;
			this.top = new_node;
		}
		else
		{
			process.stdout.write("Memory overflow\n");
		}
	}
	//Add a top element in stack
	pop()
	{
		var temp = "";
		if (this.top != null)
		{
			temp = this.top.data;
			this.top = this.top.next;
		}
		return temp;
	}
	is_empty()
	{
		if (this.top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	peek()
	{
		if (this.top != null)
		{
			return this.top.data;
		}
		else
		{
			return "";
		}
	}
	//Converting the given prefix expression to infix expression
	infix_conversion(prefix)
	{
		//Get the size
		var size = prefix.length;
		//Create stack object
		var collection = new MyStack();
		//Some useful variables which is using of to storing current result
		var auxiliary = "";
		var op1 = "";
		var op2 = "";
		var is_valid = true;
		for (var i = size - 1; i >= 0; i--)
		{
			//Check whether given prefix location at [i] is an operator or not
			if (prefix[i] == '+' || prefix[i] == '-' || prefix[i] == '*' || prefix[i] == '/' || prefix[i] == '^' || prefix[i] == '%')
			{
				// When operator exist
				// Check that two operands exist or not
				if (collection.is_two_operands() == true)
				{
					op1 = collection.pop();
					op2 = collection.pop();
					auxiliary = "(" + op1 + prefix[i] + op2 + ")";
					collection.push(auxiliary);
				}
				else
				{
					is_valid = false;
				}
			}
			else if (((prefix[i]).charCodeAt(0) >= ('0').charCodeAt(0) && (prefix[i]).charCodeAt(0) <= ('9').charCodeAt(0)) || ((prefix[i]).charCodeAt(0) >= ('a').charCodeAt(0) && (prefix[i]).charCodeAt(0) <= ('z').charCodeAt(0)) || ((prefix[i]).charCodeAt(0) >= ('A').charCodeAt(0) && (prefix[i]).charCodeAt(0) <= ('Z').charCodeAt(0)))
			{
				auxiliary = "" + prefix[i];
				collection.push(auxiliary);
			}
			else
			{
				is_valid = false;
			}
		}
		if (is_valid == false)
		{
			process.stdout.write("Invalid Prefix : " + prefix);
		}
		else
		{
			process.stdout.write("\n Prefix : " + prefix);
			process.stdout.write("\n Infix  : " + collection.pop());
		}
	}
}

function main()
{
	var obj = new MyStack();
	var prefix = "+*ab^cd";
	obj.infix_conversion(prefix);
	prefix = "-+*^%adcex*y^ab";
	obj.infix_conversion(prefix);
}
main();

Output

 Prefix : +*ab^cd
 Infix  : ((a*b)+(c^d))
 Prefix : -+*^%adcex*y^ab
 Infix  : (((((a%d)^c)*e)+x)-(y*(a^b)))
#  Python 3 program
#  Prefix to infix conversion

# Stack Node
class Node :
	
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class MyStack :
	
	def __init__(self) :
		self.top = None
	
	# Check two operands exist or not
	def is_two_operands(self) :
		counter = 0
		temp = self.top
		while (temp != None and counter < 2) :
			counter += 1
			temp = temp.next
		
		return counter == 2
	
	# Add a new element in stack
	def push(self, data) :
		# Make a new stack node
		new_node = Node(data)
		if (new_node != None) :
			new_node.next = self.top
			self.top = new_node
		else :
			print("Memory overflow\n", end = "")
		
	
	# Add a top element in stack
	def pop(self) :
		temp = ""
		if (self.top != None) :
			temp = self.top.data
			self.top = self.top.next
		
		return temp
	
	def is_empty(self) :
		if (self.top != None) :
			return False
		else :
			return True
		
	
	# Used to get top element of stack
	def peek(self) :
		if (self.top != None) :
			return self.top.data
		else :
			return ""
		
	
	# Converting the given prefix expression to infix expression
	def infix_conversion(self, prefix) :
		# Get the size
		size = len(prefix)
		# Create stack object
		collection = MyStack()
		# Some useful variables which is using of to storing current result
		auxiliary = ""
		op1 = ""
		op2 = ""
		is_valid = True
		i = size - 1
		while (i >= 0) :
			# Check whether given prefix location at [i] is an operator or not
			if (prefix[i] == '+'
				or prefix[i] == '-'
				or prefix[i] == '*'
				or prefix[i] == '/'
				or prefix[i] == '^'
				or prefix[i] == '%') :
				#  When operator exist
				#  Check that two operands exist or not
				if (collection.is_two_operands() == True) :
					op1 = collection.pop()
					op2 = collection.pop()
					auxiliary = "("+ op1 + prefix[i] + op2 +")"
					collection.push(auxiliary)
				else :
					is_valid = False
				
			
			elif((ord(prefix[i]) >= ord('0') and ord(prefix[i]) <= ord('9')) or(ord(prefix[i]) >= ord('a') and ord(prefix[i]) <= ord('z')) or(ord(prefix[i]) >= ord('A') and ord(prefix[i]) <= ord('Z'))) :
				auxiliary = prefix[i]
				collection.push(auxiliary)
			else :
				is_valid = False
			
			i -= 1
		
		if (is_valid == False) :
			print("Invalid Prefix : ", prefix, end = "")
		else :
			print("\n Prefix : ", prefix, end = "")
			print("\n Infix  : ", collection.pop(), end = "")
		
	

def main() :
	obj = MyStack()
	prefix = "+*ab^cd"
	obj.infix_conversion(prefix)
	prefix = "-+*^%adcex*y^ab"
	obj.infix_conversion(prefix)

if __name__ == "__main__": main()

Output

 Prefix :  +*ab^cd
 Infix  :  ((a*b)+(c^d))
 Prefix :  -+*^%adcex*y^ab
 Infix  :  (((((a%d)^c)*e)+x)-(y*(a^b)))
#  Ruby program
#  Prefix to infix conversion

# Stack Node
class Node 

	# Define the accessor and reader of class Node  
	attr_reader :data, :next
	attr_accessor :data, :next

	def initialize(data)
		self.data = data
		self.next = nil
	end
end
class MyStack 

	# Define the accessor and reader of class MyStack  
	attr_reader :top
	attr_accessor :top

	def initialize()
		self.top = nil
	end
	# Check two operands exist or not
	def is_two_operands()
	
		counter = 0
		temp = self.top
		while (temp != nil && counter < 2)
		
			counter += 1
			temp = temp.next
		end
		return counter == 2
	end
	# Add a new element in stack
	def push(data)
	
		# Make a new stack node
		new_node = Node.new(data)
		if (new_node != nil)
		
			new_node.next = @top
			@top = new_node
		else
		
			print("Memory overflow\n")
		end
	end
	# Add a top element in stack
	def pop()
	
		temp = ""
		if (@top != nil)
		
			temp = @top.data
			@top = @top.next
		end
		return temp
	end
	def is_empty()
	
		if (self.top != nil)
		
			return false
		else
		
			return true
		end
	end
	# Used to get top element of stack
	def peek()
	
		if (@top != nil)
		
			return @top.data
		else
		
			return ""
		end
	end
	# Converting the given prefix expression to infix expression
	def infix_conversion(prefix)
	
		# Get the size
		size = prefix.length()
		# Create stack object
		collection = MyStack.new()
		# Some useful variables which is using of to storing current result
		auxiliary = ""
		op1 = ""
		op2 = ""
		is_valid = true
		i = size - 1
		while (i >= 0)
		
			# Check whether given prefix location at [i] is an operator or not
			if (prefix[i] == '+' || prefix[i] == '-' || prefix[i] == '*' || prefix[i] == '/' || prefix[i] == '^' || prefix[i] == '%')
			
				#  When operator exist
				#  Check that two operands exist or not
				if (collection.is_two_operands() == true)
				
					op1 = collection.pop()
					op2 = collection.pop()
					auxiliary = "("+ op1 + prefix[i] + op2 +")"
					collection.push(auxiliary)
				else
				
					is_valid = false
				end
			elsif(((prefix[i]).ord >= ('0').ord && (prefix[i]).ord <= ('9').ord) || ((prefix[i]).ord >= ('a').ord && (prefix[i]).ord <= ('z').ord) || ((prefix[i]).ord >= ('A').ord && (prefix[i]).ord <= ('Z').ord))
			
				auxiliary =  prefix[i]
				collection.push(auxiliary)
			else
			
				is_valid = false
			end
			i -= 1
		end
		if (is_valid == false)
		
			print("Invalid Prefix : ", prefix)
		else
		
			print("\n Prefix : ", prefix)
			print("\n Infix  : ", collection.pop())
		end
	end
end
def main()

	obj = MyStack.new()
	prefix = "+*ab^cd"
	obj.infix_conversion(prefix)
	prefix = "-+*^%adcex*y^ab"
	obj.infix_conversion(prefix)
end
main()

Output

 Prefix : +*ab^cd
 Infix  : ((a*b)+(c^d))
 Prefix : -+*^%adcex*y^ab
 Infix  : (((((a%d)^c)*e)+x)-(y*(a^b)))
// Scala program
// Prefix to infix conversion
//Stack Node
class Node(var data: String,
	var next: Node)
{
	def this(data: String)
	{
		this(data, null);
	}
}
class MyStack(var top: Node)
{
	def this()
	{
		this(null);
	}
	//Check two operands exist or not
	def is_two_operands(): Boolean = {
		var counter: Int = 0;
		var temp: Node = this.top;
		while (temp != null && counter < 2)
		{
			counter += 1;
			temp = temp.next;
		}
		return counter == 2;
	}
	//Add a new element in stack
	def push(data: String): Unit = {
		//Make a new stack node
		var new_node: Node = new Node(data);
		if (new_node != null)
		{
			new_node.next = top;
			top = new_node;
		}
		else
		{
			print("Memory overflow\n");
		}
	}
	//Add a top element in stack
	def pop(): String = {
		var temp: String = "";
		if (top != null)
		{
			temp = top.data;
			top = top.next;
		}
		return temp;
	}
	def is_empty(): Boolean = {
		if (this.top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	def peek(): String = {
		if (top != null)
		{
			return top.data;
		}
		else
		{
			return "";
		}
	}
	//Converting the given prefix expression to infix expression
	def infix_conversion(prefix: String): Unit = {
		//Get the size
		var size: Int = prefix.length();
		//Create stack object
		var collection: MyStack = new MyStack();
		//Some useful variables which is using of to storing current result
		var auxiliary: String = "";
		var op1: String = "";
		var op2: String = "";
		var is_valid: Boolean = true;
		var i: Int = size - 1;
		while (i >= 0)
		{
			//Check whether given prefix location at [i] is an operator or not
			if (prefix(i) == '+' || prefix(i) == '-' || prefix(i) == '*' || prefix(i) == '/' || prefix(i) == '^' || prefix(i) == '%')
			{
				// When operator exist
				// Check that two operands exist or not
				if (collection.is_two_operands() == true)
				{
					op1 = collection.pop();
					op2 = collection.pop();
					auxiliary = "(" + op1 + prefix(i) + op2 + ")";
					collection.push(auxiliary);
				}
				else
				{
					is_valid = false;
				}
			}
			else if ((prefix(i) >= '0' && prefix(i) <= '9') || (prefix(i) >= 'a' && prefix(i) <= 'z') || (prefix(i) >= 'A' && prefix(i) <= 'Z'))
			{
				auxiliary = "" + prefix(i);
				collection.push(auxiliary);
			}
			else
			{
				is_valid = false;
			}
			i -= 1;
		}
		if (is_valid == false)
		{
			print("Invalid Prefix : " + prefix);
		}
		else
		{
			print("\n Prefix : " + prefix);
			print("\n Infix  : " + collection.pop());
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyStack = new MyStack();
		var prefix: String = "+*ab^cd";
		obj.infix_conversion(prefix);
		prefix = "-+*^%adcex*y^ab";
		obj.infix_conversion(prefix);
	}
}

Output

 Prefix : +*ab^cd
 Infix  : ((a*b)+(c^d))
 Prefix : -+*^%adcex*y^ab
 Infix  : (((((a%d)^c)*e)+x)-(y*(a^b)))
// Swift program
// Prefix to infix conversion
//Stack Node
class Node
{
	var data: String;
	var next: Node? ;
	init(_ data: String)
	{
		self.data = data;
		self.next = nil;
	}
}
class MyStack
{
	var top: Node? ;
	init()
	{
		self.top = nil;
	}
	//Check two operands exist or not
	func is_two_operands() -> Bool
	{
		var counter: Int = 0;
		var temp: Node? = self.top;
		while (temp != nil && counter < 2)
		{
			counter += 1;
			temp = temp!.next;
		}
		return counter == 2;
	}
	//Add a new element in stack
	func push(_ data: String)
	{
		//Make a new stack node
		let new_node: Node? = Node(data);
		if (new_node != nil)
		{
			new_node!.next = self.top;
			self.top = new_node;
		}
		else
		{
			print("Memory overflow\n", terminator: "");
		}
	}
	//Add a top element in stack
	func pop() -> String
	{
		var temp: String = "";
		if (self.top != nil)
		{
			temp = self.top!.data;
			self.top = self.top!.next;
		}
		return temp;
	}
	func is_empty() -> Bool
	{
		if (self.top != nil)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	func peek() -> String
	{
		if (self.top != nil)
		{
			return self.top!.data;
		}
		else
		{
			return "";
		}
	}
	//Converting the given prefix expression to infix expression
	func infix_conversion(_ exp: String)
	{
      	let prefix = Array(exp);
		//Get the size
		let size: Int = prefix.count;
		//Create stack object
		let collection: MyStack = MyStack();
		//Some useful variables which is using of to storing current result
		var auxiliary: String = "";
		var op1: String = "";
		var op2: String = "";
		var is_valid: Bool = true;
		var i: Int = size - 1;
		while (i >= 0)
		{
			//Check whether given prefix location at [i] is an operator or not
			if (prefix[i] == "+" || prefix[i] == "-" || prefix[i] == "*" || prefix[i] == "/" || prefix[i] == "^" || prefix[i] == "%")
			{
				// When operator exist
				// Check that two operands exist or not
				if (collection.is_two_operands() == true)
				{
					op1 = collection.pop();
					op2 = collection.pop();
					auxiliary = "(" + op1 + String(prefix[i]) + op2 + ")";
					collection.push(auxiliary);
				}
				else
				{
					is_valid = false;
				}
			}
			else if ((prefix[i] >= "0" && prefix[i] <= "9") || (prefix[i] >= "a" && prefix[i] <= "z") || (prefix[i] >= "A" && prefix[i] <= "Z"))
			{
				auxiliary =  String(prefix[i]);
				collection.push(auxiliary);
			}
			else
			{
				is_valid = false;
			}
			i -= 1;
		}
		if (is_valid == false)
		{
			print("Invalid Prefix : ", exp, terminator: "");
		}
		else
		{
			print("\n Prefix : ", exp, terminator: "");
			print("\n Infix  : ", collection.pop(), terminator: "");
		}
	}
}
func main()
{
	let obj: MyStack = MyStack();
	var prefix: String = "+*ab^cd";
	obj.infix_conversion(prefix);
	prefix = "-+*^%adcex*y^ab";
	obj.infix_conversion(prefix);
}
main();

Output

 Prefix :  +*ab^cd
 Infix  :  ((a*b)+(c^d))
 Prefix :  -+*^%adcex*y^ab
 Infix  :  (((((a%d)^c)*e)+x)-(y*(a^b)))

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







© 2021, kalkicode.com, All rights reserved