Postfix To Prefix Conversion

Here given code implementation process.

// Java program
// Postfix to Prefix 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;
	}
	//Count number of elements in stack 
	public int is_size()
	{
		int counter = 0;
		Node temp = this.top;
		while (temp != null)
		{
			counter++;
			temp = temp.next;
		}
		return counter;
	}
	//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 "";
		}
	}
	public void prefix(String postfix)
	{
		int size = postfix.length();
		MyStack collection = new MyStack();
		String auxiliary = "";
		for (int i = 0; i < size; ++i)
		{
			//Check whether given postfix location at [i] is an operator or not
			if (postfix.charAt(i) == '+' 
                || postfix.charAt(i) == '-' 
                || postfix.charAt(i) == '*' 
                || postfix.charAt(i) == '/' 
                || postfix.charAt(i) == '^')
			{
				//When operator exist
				//Check stack size
				if (collection.is_empty()==false)
				{
					auxiliary = collection.pop();
					auxiliary = collection.pop() + auxiliary;
					auxiliary = postfix.charAt(i) + auxiliary;
					collection.push(auxiliary);
				}
			}
			else if ((postfix.charAt(i) >= '0' 
                      && postfix.charAt(i) <= '9') 
                     || (postfix.charAt(i) >= 'a' 
                         && postfix.charAt(i) <= 'z') 
                     || (postfix.charAt(i) >= 'A' 
                         && postfix.charAt(i) <= 'Z'))
			{
				auxiliary = ""+postfix.charAt(i);
				collection.push(auxiliary);
			}
			else
			{
				System.out.print("Invalid Postfix" + postfix + "\n" + postfix.charAt(i));
				return;
			}
		}
      	System.out.print("\nPostfix : "+ postfix);
		System.out.print("\nPrefix  : "+ collection.pop());
	}
	public static void main(String[] args)
	{
		MyStack obj = new MyStack();
		String postfix = "ab+c*ef+g/+";
		obj.prefix(postfix);
	}
}

Output

Postfix : ab+c*ef+g/+
Prefix  : +*+abc/+efg
//Include header file
#include <iostream>

#include<string.h>

using namespace std;
// C++ program
// Postfix to Prefix 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;
	}
	//Count number of elements in stack 
	int is_size()
	{
		int counter = 0;
		Node * temp = this->top;
		while (temp != NULL)
		{
			counter++;
			temp = temp->next;
		}
		return counter;
	}
	//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 "";
		}
	}
	void prefix(string postfix)
	{
		int size = postfix.size();
		MyStack collection = MyStack();
		string auxiliary = "";
		for (int i = 0; i < size; ++i)
		{
			//Check whether given postfix location at [i] is an operator or not
			if (postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/' || postfix[i] == '^')
			{
				//When operator exist
				//Check stack size
				if (collection.is_empty() == false)
				{
					auxiliary = collection.pop();
					auxiliary = collection.pop() + auxiliary;
					auxiliary = postfix[i] + auxiliary;
					collection.push(auxiliary);
				}
			}
			else if ((postfix[i] >= '0' && postfix[i] <= '9') || (postfix[i] >= 'a' && postfix[i] <= 'z') || (postfix[i] >= 'A' && postfix[i] <= 'Z'))
			{
				auxiliary = postfix[i];
				collection.push(auxiliary);
			}
			else
			{
				cout << "Invalid Postfix" << postfix << "\n" << postfix[i];
				return;
			}
		}
		cout << "\nPostfix : " << postfix;
		cout << "\nPrefix  : " << collection.pop();
	}
};
int main()
{
	MyStack obj = MyStack();
	string postfix = "ab+c*ef+g/+";
	obj.prefix(postfix);
	return 0;
}

Output

Postfix : ab+c*ef+g/+
Prefix  : +*+abc/+efg
//Include namespace system
using System;
// C# program
// Postfix to Prefix 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;
	}
	//Count number of elements in stack 
	public int is_size()
	{
		int counter = 0;
		Node temp = this.top;
		while (temp != null)
		{
			counter++;
			temp = temp.next;
		}
		return counter;
	}
	//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 "";
		}
	}
	public void prefix(String postfix)
	{
		int size = postfix.Length;
		MyStack collection = new MyStack();
		String auxiliary = "";
		for (int i = 0; i < size; ++i)
		{
			//Check whether given postfix location at [i] is an operator or not
			if (postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/' || postfix[i] == '^')
			{
				//When operator exist
				//Check stack size
				if (collection.is_empty() == false)
				{
					auxiliary = collection.pop();
					auxiliary = collection.pop() + auxiliary;
					auxiliary = postfix[i] + auxiliary;
					collection.push(auxiliary);
				}
			}
			else if ((postfix[i] >= '0' && postfix[i] <= '9') || (postfix[i] >= 'a' && postfix[i] <= 'z') || (postfix[i] >= 'A' && postfix[i] <= 'Z'))
			{
				auxiliary = "" + postfix[i];
				collection.push(auxiliary);
			}
			else
			{
				Console.Write("Invalid Postfix" + postfix + "\n" + postfix[i]);
				return;
			}
		}
		Console.Write("\nPostfix : " + postfix);
		Console.Write("\nPrefix  : " + collection.pop());
	}
	public static void Main(String[] args)
	{
		MyStack obj = new MyStack();
		String postfix = "ab+c*ef+g/+";
		obj.prefix(postfix);
	}
}

Output

Postfix : ab+c*ef+g/+
Prefix  : +*+abc/+efg
<?php
// Php program
// Postfix to Prefix 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;
	}
	//Count number of elements in stack 
	public	function is_size()
	{
		$counter = 0;
		$temp = $this->top;
		while ($temp != null)
		{
			$counter++;
			$temp = $temp->next;
		}
		return $counter;
	}
	//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 "";
		}
	}
	public	function prefix($postfix)
	{
		$size = strlen($postfix);
		$collection = new MyStack();
		$auxiliary = "";
		for ($i = 0; $i < $size; ++$i)
		{
			//Check whether given postfix location at [i] is an operator or not
			if ($postfix[$i] == '+' || $postfix[$i] == '-' || $postfix[$i] == '*' || $postfix[$i] == '/' || $postfix[$i] == '^')
			{
				//When operator exist
				//Check stack size
				if ($collection->is_empty() == false)
				{
					$auxiliary = $collection->pop();
					$auxiliary = $collection->pop() . $auxiliary;
					$auxiliary = $postfix[$i] . $auxiliary;
					$collection->push($auxiliary);
				}
			}
			else if ((ord($postfix[$i]) >= ord('0') && ord($postfix[$i]) <= ord('9')) || (ord($postfix[$i]) >= ord('a') && ord($postfix[$i]) <= ord('z')) || (ord($postfix[$i]) >= ord('A') && ord($postfix[$i]) <= ord('Z')))
			{
				$auxiliary = "". $postfix[$i];
				$collection->push($auxiliary);
			}
			else
			{
				echo "Invalid Postfix". $postfix ."\n". $postfix[$i];
				return;
			}
		}
		echo "\nPostfix : ". $postfix;
		echo "\nPrefix  : ". $collection->pop();
	}
}

function main()
{
	$obj = new MyStack();
	$postfix = "ab+c*ef+g/+";
	$obj->prefix($postfix);
}
main();

Output

Postfix : ab+c*ef+g/+
Prefix  : +*+abc/+efg
// Node Js program
// Postfix to Prefix Conversion
//Stack Node
class Node
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class MyStack
{
	constructor()
	{
		this.top = null;
	}
	//Count number of elements in stack 
	is_size()
	{
		var counter = 0;
		var temp = this.top;
		while (temp != null)
		{
			counter++;
			temp = temp.next;
		}
		return counter;
	}
	//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 "";
		}
	}
	prefix(postfix)
	{
		var size = postfix.length;
		var collection = new MyStack();
		var auxiliary = "";
		for (var i = 0; i < size; ++i)
		{
			//Check whether given postfix location at [i] is an operator or not
			if (postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/' || postfix[i] == '^')
			{
				//When operator exist
				//Check stack size
				if (collection.is_empty() == false)
				{
					auxiliary = collection.pop();
					auxiliary = collection.pop() + auxiliary;
					auxiliary = postfix[i] + auxiliary;
					collection.push(auxiliary);
				}
			}
			else if (((postfix[i]).charCodeAt(0) >= ('0').charCodeAt(0) && (postfix[i]).charCodeAt(0) <= ('9').charCodeAt(0)) || ((postfix[i]).charCodeAt(0) >= ('a').charCodeAt(0) && (postfix[i]).charCodeAt(0) <= ('z').charCodeAt(0)) || ((postfix[i]).charCodeAt(0) >= ('A').charCodeAt(0) && (postfix[i]).charCodeAt(0) <= ('Z').charCodeAt(0)))
			{
				auxiliary = postfix[i];
				collection.push(auxiliary);
			}
			else
			{
				process.stdout.write("Invalid Postfix" + postfix + "\n" + postfix[i]);
				return;
			}
		}
		process.stdout.write("\nPostfix : " + postfix);
		process.stdout.write("\nPrefix  : " + collection.pop());
	}
}

function main()
{
	var obj = new MyStack();
	var postfix = "ab+c*ef+g/+";
	obj.prefix(postfix);
}
main();

Output

Postfix : ab+c*ef+g/+
Prefix  : +*+abc/+efg
#  Python 3 program
#  Postfix to Prefix Conversion

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

class MyStack :
	
	def __init__(self) :
		self.top = None
	
	# Count number of elements in stack 
	def is_size(self) :
		counter = 0
		temp = self.top
		while (temp != None) :
			counter += 1
			temp = temp.next
		
		return counter
	
	# 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 ""
		
	
	def prefix(self, postfix) :
		size = len(postfix)
		collection = MyStack()
		auxiliary = ""
		i = 0
		while (i < size) :
			# Check whether given postfix location at [i] is an operator or not
			if (postfix[i] == '+'
				or postfix[i] == '-'
				or postfix[i] == '*'
				or postfix[i] == '/'
				or postfix[i] == '^') :
				# When operator exist
				# Check stack size
				if (collection.is_empty() == False) :
					auxiliary = collection.pop()
					auxiliary = collection.pop() + auxiliary
					auxiliary = postfix[i] + auxiliary
					collection.push(auxiliary)
				
			
			elif((ord(postfix[i]) >= ord('0') and ord(postfix[i]) <= ord('9')) or(ord(postfix[i]) >= ord('a') and ord(postfix[i]) <= ord('z')) or(ord(postfix[i]) >= ord('A') and ord(postfix[i]) <= ord('Z'))) :
				auxiliary = ""+ postfix[i]
				collection.push(auxiliary)
			else :
				print("Invalid Postfix", postfix ,"\n", postfix[i], end = "")
				return
			
			i += 1
		
		print("\nPostfix : ", postfix, end = "")
		print("\nPrefix  : ", collection.pop(), end = "")
	

def main() :
	obj = MyStack()
	postfix = "ab+c*ef+g/+"
	obj.prefix(postfix)

if __name__ == "__main__": main()

Output

Postfix :  ab+c*ef+g/+
Prefix  :  +*+abc/+efg
#  Ruby program
#  Postfix to Prefix 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
	# Count number of elements in stack 
	def is_size()
	
		counter = 0
		temp = self.top
		while (temp != nil)
		
			counter += 1
			temp = temp.next
		end
		return counter
	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
	def prefix(postfix)
	
		size = postfix.length()
		collection = MyStack.new()
		auxiliary = ""
		i = 0
		while (i < size)
		
			# Check whether given postfix location at [i] is an operator or not
			if (postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/' || postfix[i] == '^')
			
				# When operator exist
				# Check stack size
				if (collection.is_empty() == false)
				
					auxiliary = collection.pop()
					auxiliary = collection.pop() + auxiliary
					auxiliary = postfix[i] + auxiliary
					collection.push(auxiliary)
				end
			elsif(((postfix[i]).ord >= ('0').ord && (postfix[i]).ord <= ('9').ord) || ((postfix[i]).ord >= ('a').ord && (postfix[i]).ord <= ('z').ord) || ((postfix[i]).ord >= ('A').ord && (postfix[i]).ord <= ('Z').ord))
			
				auxiliary = ""+ postfix[i]
				collection.push(auxiliary)
			else
			
				print("Invalid Postfix", postfix ,"\n", postfix[i])
				return
			end
			i += 1
		end
		print("\nPostfix : ", postfix)
		print("\nPrefix  : ", collection.pop())
	end
end
def main()

	obj = MyStack.new()
	postfix = "ab+c*ef+g/+"
	obj.prefix(postfix)
end
main()

Output

Postfix : ab+c*ef+g/+
Prefix  : +*+abc/+efg
// Scala program
// Postfix to Prefix 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);
	}
	//Count number of elements in stack 
	def is_size(): Int = {
		var counter: Int = 0;
		var temp: Node = this.top;
		while (temp != null)
		{
			counter += 1;
			temp = temp.next;
		}
		return counter;
	}
	//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 "";
		}
	}
	def prefix(postfix: String): Unit = {
		var size: Int = postfix.length();
		var collection: MyStack = new MyStack();
		var auxiliary: String = "";
		var i: Int = 0;
		while (i < size)
		{
			//Check whether given postfix location at [i] is an operator or not
			if (postfix(i) == '+' || postfix(i) == '-' || postfix(i) == '*' || postfix(i) == '/' || postfix(i) == '^')
			{
				//When operator exist
				//Check stack size
				if (collection.is_empty() == false)
				{
					auxiliary = collection.pop();
					auxiliary = collection.pop() + auxiliary;
					auxiliary = ""+postfix(i) + auxiliary;
					collection.push(auxiliary);
				}
			}
			else if ((postfix(i) >= '0' && postfix(i) <= '9') || (postfix(i) >= 'a' && postfix(i) <= 'z') || (postfix(i) >= 'A' && postfix(i) <= 'Z'))
			{
				auxiliary = "" + postfix(i);
				collection.push(auxiliary);
			}
			else
			{
				print("Invalid Postfix" + postfix + "\n" + postfix(i));
				return;
			}
			i += 1;
		}
		print("\nPostfix : " + postfix);
		print("\nPrefix  : " + collection.pop());
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyStack = new MyStack();
		var postfix: String = "ab+c*ef+g/+";
		obj.prefix(postfix);
	}
}

Output

Postfix : ab+c*ef+g/+
Prefix  : +*+abc/+efg
// Swift program
// Postfix to Prefix 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;
	}
	//Count number of elements in stack 
	func is_size() -> Int
	{
		var counter: Int = 0;
		var temp: Node? = self.top;
		while (temp != nil)
		{
			counter += 1;
			temp = temp!.next;
		}
		return counter;
	}
	//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 "";
		}
	}
	func prefix(_ text: String)
	{
      	let postfix = Array(text);
		let size: Int = postfix.count;
		let collection: MyStack = MyStack();
		var auxiliary: String = "";
		var i: Int = 0;
		while (i < size)
		{
			//Check whether given postfix location at [i] is an operator or not
			if (postfix[i] == "+" || postfix[i] == "-" || postfix[i] == "*" || postfix[i] == "/" || postfix[i] == "^")
			{
				//When operator exist
				//Check stack size
				if (collection.is_empty() == false)
				{
					auxiliary = collection.pop();
					auxiliary = collection.pop() + auxiliary;
					auxiliary = String(postfix[i]) + auxiliary;
					collection.push(auxiliary);
				}
			}
			else if ((postfix[i] >= "0" && postfix[i] <= "9") || (postfix[i] >= "a" && postfix[i] <= "z") || (postfix[i] >= "A" && postfix[i] <= "Z"))
			{
				auxiliary = String(postfix[i]);
				collection.push(auxiliary);
			}
			else
			{
				print("Invalid Postfix", postfix ,"\n", postfix[i], terminator: "");
				return;
			}
			i += 1;
		}
		print("\nPostfix : ", text, terminator: "");
		print("\nPrefix  : ", collection.pop(), terminator: "");
	}
}
func main()
{
	let obj: MyStack = MyStack();
	let postfix: String = "ab+c*ef+g/+";
	obj.prefix(postfix);
}
main();

Output

Postfix :  ab+c*ef+g/+
Prefix  :  +*+abc/+efg

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