Evaluate postfix expression

Here given code implementation process.

// C Program
// Evaluate postfix expression
#include <stdio.h>

#include <stdlib.h>

struct MyStack
{
	int data;
	struct MyStack * next;
};
//Add element into stack
void push(int data, struct MyStack ** top)
{
	//Create a new stack element
	struct MyStack * new_node = (struct MyStack *) malloc(sizeof(struct MyStack));
	if (new_node != NULL)
	{
		new_node->data = data;
		new_node->next = *top;
      	*top = new_node;
	}
	else
	{
		printf("\nMemory Overflow\n");
	}
}
int pop(struct MyStack **top)
{
	int value = 0;
	if ( * top != NULL)
	{
		struct MyStack *temp = *top;
		value = temp->data;* top = temp->next;
		free(temp);
		temp = NULL;
	}
	return value;
}
int is_empty(struct MyStack *top)
{
	if (top != NULL)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}
void evaluate(char expression[], int size)
{
	struct MyStack * top = NULL;
	int a = 0, b = 0;
	for (int i = 0; i < size; i++)
	{
		if (expression[i] >= '0' && expression[i] <= '9')
		{
			a = expression[i] - '0';
			push(a, & top);
		}
		else if (!is_empty(top))
		{
			a = pop( & top);
			b = pop( & top);
			//Perform arithmetic operations 
			if (expression[i] == '+')
			{
				push(b + a, & top);
			}
			else if (expression[i] == '-')
			{
				push(b - a, & top);
			}
			else if (expression[i] == '*')
			{
				push(b * a, & top);
			}
			else if (expression[i] == '/')
			{
				push(b / a, & top);
			}
		}
	}
	printf("%s = %d\n", expression, pop( & top));
}
int main()
{
	// (1+2)*(3+5)
	char postfix1[] = "12+35+*";
	int size = sizeof(postfix1) / sizeof(postfix1[0]) - 1;
	evaluate(postfix1, size);
	char postfix2[] = "72*62/+4+-";
	size = sizeof(postfix2) / sizeof(postfix2[0]) - 1;
	evaluate(postfix2, size);
	return 0;
}

Output

12+35+* = 24
72*62/+4+- = -21
/*
  Java Program
  Evaluate postfix expression
*/
//Stack Node
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		this.data = data;
		this.next = null;
	}
}
class MyStack
{
	public Node top;
	public MyStack()
	{
		top = null;
	}
	//Add a new element in stack
	public void push(int 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
	//When stack is empty it returns -1
	public int pop()
	{
		int temp = 0;
		if (top != null)
		{
			temp = top.data;
			top = top.next;
		}
		return temp;
	}
	public boolean is_empty()
	{
		if (this.top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	public void evaluate(String expression)
	{
		//Get the length of expression
		int size = expression.length();
		int a = 0, b = 0;
		for (int i = 0; i < size; i++)
		{
			if (expression.charAt(i) >= '0' && expression.charAt(i) <= '9')
			{
				a = expression.charAt(i) - '0';
				push(a);
			}
			else if (!is_empty())
			{
				a = pop();
				b = pop();
				//Perform arithmetic operations 
				if (expression.charAt(i) == '+')
				{
					push(b + a);
				}
				else if (expression.charAt(i) == '-')
				{
					push(b - a);
				}
				else if (expression.charAt(i) == '*')
				{
					push(b * a);
				}
				else if (expression.charAt(i) == '/')
				{
					push(b / a);
				}
			}
		}
		System.out.print(expression + " = " + pop() + "\n");
	}
	public static void main(String[] args)
	{
		MyStack obj = new MyStack();
		//Execute the following expression
		obj.evaluate("12+35+*");
		obj.evaluate("72*62/+4+-");
	}
}

Output

12+35+* = 24
72*62/+4+- = -21
/*
  C++ Program
  Evaluate postfix expression
*/
//Stack Node
#include<iostream>

using namespace std;
class Node
{
	public: int data;
	Node * next;
	Node(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class MyStack
{
	public: Node * top;
	MyStack()
	{
		this->top = NULL;
	}
	//Add a new element in stack
	void push(int 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
	//When stack is empty it returns -1
	int pop()
	{
		int temp = 0;
		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;
		}
	}
	void evaluate(string expression)
	{
		//Get the length of expression
		int size = expression.size();
		int a = 0, b = 0;
		for (int i = 0; i < size; i++)
		{
			if (expression[i] >= '0' && expression[i] <= '9')
			{
				a = expression[i] - '0';
				this->push(a);
			}
			else if (!this->is_empty())
			{
				a = this->pop();
				b = this->pop();
				//Perform arithmetic operations 
				if (expression[i] == '+')
				{
					this->push(b + a);
				}
				else if (expression[i] == '-')
				{
					this->push(b - a);
				}
				else if (expression[i] == '*')
				{
					this->push(b * a);
				}
				else if (expression[i] == '/')
				{
					this->push(b / a);
				}
			}
		}
		cout << expression << " = " << this->pop() << "\n";
	}
};
int main()
{
	MyStack obj =  MyStack();
	//Execute the following expression
	obj.evaluate("12+35+*");
	obj.evaluate("72*62/+4+-");
	return 0;
}

Output

12+35+* = 24
72*62/+4+- = -21
/*
  C# Program
  Evaluate postfix expression
*/
//Stack Node
using System;
public class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class MyStack
{
	public Node top;
	public MyStack()
	{
		top = null;
	}
	//Add a new element in stack
	public void push(int 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
	//When stack is empty it returns -1
	public int pop()
	{
		int temp = 0;
		if (top != null)
		{
			temp = top.data;
			top = top.next;
		}
		return temp;
	}
	public Boolean is_empty()
	{
		if (this.top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	public void evaluate(String expression)
	{
		//Get the length of expression
		int size = expression.Length;
		int a = 0, b = 0;
		for (int i = 0; i < size; i++)
		{
			if (expression[i] >= '0' && expression[i] <= '9')
			{
				a = expression[i] - '0';
				push(a);
			}
			else if (!is_empty())
			{
				a = pop();
				b = pop();
				//Perform arithmetic operations 
				if (expression[i] == '+')
				{
					push(b + a);
				}
				else if (expression[i] == '-')
				{
					push(b - a);
				}
				else if (expression[i] == '*')
				{
					push(b * a);
				}
				else if (expression[i] == '/')
				{
					push(b / a);
				}
			}
		}
		Console.Write(expression + " = " + pop() + "\n");
	}
	public static void Main(String[] args)
	{
		MyStack obj = new MyStack();
		//Execute the following expression
		obj.evaluate("12+35+*");
		obj.evaluate("72*62/+4+-");
	}
}

Output

12+35+* = 24
72*62/+4+- = -21
<?php
/*
  Php Program
  Evaluate postfix expression
*/
//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;
	}
	//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
	//When stack is empty it returns -1
	public function pop()
	{
		$temp = 0;
		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;
		}
	}
	public function evaluate($expression)
	{
		//Get the length of expression
		$size = strlen($expression);
		$a = 0;
		$b = 0;
		for ($i = 0; $i < $size; $i++)
		{
			if (ord($expression[$i]) >= ord('0') && ord($expression[$i]) <= ord('9'))
			{
				$a = ord($expression[$i]) - ord('0');
				$this->push($a);
			}
			else if (!$this->is_empty())
			{
				$a = $this->pop();
				$b = $this->pop();
				//Perform arithmetic operations 
				if ($expression[$i] == '+')
				{
					$this->push($b + $a);
				}
				else if ($expression[$i] == '-')
				{
					$this->push($b - $a);
				}
				else if ($expression[$i] == '*')
				{
					$this->push($b * $a);
				}
				else if ($expression[$i] == '/')
				{
					$this->push(intval($b / $a));
				}
			}
		}
		echo($expression ." = ". $this->pop() ."\n");
	}
}

function main()
{
	$obj = new MyStack();
	//Execute the following expression
	$obj->evaluate("12+35+*");
	$obj->evaluate("72*62/+4+-");
}
main();

Output

12+35+* = 24
72*62/+4+- = -21
/*
  Node Js Program
  Evaluate postfix expression
*/
//Stack Node
class Node
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class MyStack
{
	constructor()
	{
		this.top = null;
	}
	//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
	//When stack is empty it returns -1
	pop()
	{
		var temp = 0;
		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;
		}
	}
	evaluate(expression)
	{
		//Get the length of expression
		var size = expression.length;
		var a = 0;
		var b = 0;
		for (var i = 0; i < size; i++)
		{
			if ((expression).charCodeAt(i) >= ('0').charCodeAt(0) && (expression).charCodeAt(i) <= ('9').charCodeAt(0))
			{
				a = (expression).charCodeAt(i) - ('0').charCodeAt(0);
				this.push(a);
			}
			else if (!this.is_empty())
			{
				a = this.pop();
				b = this.pop();
				//Perform arithmetic operations 
				if (expression[i] == '+')
				{
					this.push(b + a);
				}
				else if (expression[i] == '-')
				{
					this.push(b - a);
				}
				else if (expression[i] == '*')
				{
					this.push(b * a);
				}
				else if (expression[i] == '/')
				{
					this.push(parseInt(b / a));
				}
			}
		}
		process.stdout.write(expression + " = " + this.pop() + "\n");
	}
}

function main(args)
{
	var obj = new MyStack();
	//Execute the following expression
	obj.evaluate("12+35+*");
	obj.evaluate("72*62/+4+-");
}
main();

Output

12+35+* = 24
72*62/+4+- = -21
# Python 3 Program
# Evaluate postfix expression

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

class MyStack :
	
	def __init__(self) :
		self.top = None
	
	# 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
	# When stack is empty it returns 0
	def pop(self) :
		temp = 0
		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
		
	
	def evaluate(self, expression) :
		# Get the length of expression
		size = len(expression)
		a = 0
		b = 0
		i = 0
		while (i < size) :
			if (ord(expression[i]) >= ord('0') and ord(expression[i]) <= ord('9')) :
				a = ord(expression[i]) - ord('0')
				self.push(a)
			elif (not self.is_empty()) :
				a = self.pop()
				b = self.pop()
				# Perform arithmetic operations 
				if (expression[i] == '+') :
					self.push(b + a)
				elif (expression[i] == '-') :
					self.push(b - a)
				elif (expression[i] == '*') :
					self.push(b * a)
				elif (expression[i] == '/') :
					self.push(int(b / a))
				
			
			i += 1
		
		print(expression ," = ", self.pop())
	

def main() :
	obj = MyStack()
	# Execute the following expression
	obj.evaluate("12+35+*")
	obj.evaluate("72*62/+4+-")


if __name__ == "__main__": main()

Output

12+35+*  =  24
72*62/+4+-  =  -21
#   Ruby Program
#   Evaluate postfix expression

# 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()
	
		@top = nil
	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
	# When stack is empty it returns 0
	def pop()
	
		temp = 0
		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
	def evaluate(expression)
	
		# Get the length of expression
		size = expression.length()
		a = 0
		b = 0
		i = 0
		while (i < size)
		
			if ((expression[i]).ord >= ('0').ord && (expression[i]).ord <= ('9').ord)
			
				a = (expression[i]).ord - ('0').ord
				self.push(a)
			elsif (!self.is_empty())
			
				a = self.pop()
				b = self.pop()
				# Perform arithmetic operations 
				if (expression[i] == '+')
				
					self.push(b + a)
				elsif (expression[i] == '-')
				
					self.push(b - a)
				elsif (expression[i] == '*')
				
					self.push(b * a)
				elsif (expression[i] == '/')
				
					self.push(b / a)
				end
			end
			i += 1
		end
		print(expression ," = ", self.pop() ,"\n")
	end
end
def main()

	obj = MyStack.new()
	# Execute the following expression
	obj.evaluate("12+35+*")
	obj.evaluate("72*62/+4+-")
end
main()

Output

12+35+* = 24
72*62/+4+- = -21
/*
  Scala Program
  Evaluate postfix expression
*/
//Stack Node
class Node(var data: Int,
	var next: Node)
{
	def this(data: Int)
	{
		this(data,null);
	}
}
class MyStack(var top: Node)
{
	def this()
	{
		this(null);
	}
	//Add a new element in stack
	def push(data: Int): 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
	//When stack is empty it returns 0
	def pop(): Int = {
		var temp: Int = 0;
		if (top != null)
		{
			temp = top.data;
			top = top.next;
		}
		return temp;
	}
	def is_empty(): Boolean = {
		if (this.top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	def evaluate(expression: String): Unit = {
		//Get the length of expression
		var size: Int = expression.length();
		var a: Int = 0;
		var b: Int = 0;
		var i: Int = 0;
		while (i < size)
		{
			if (expression(i) >= '0' && expression(i) <= '9')
			{
				a = expression(i) - '0';
				push(a);
			}
			else if (!is_empty())
			{
				a = pop();
				b = pop();
				//Perform arithmetic operations 
				if (expression(i) == '+')
				{
					push(b + a);
				}
				else if (expression(i) == '-')
				{
					push(b - a);
				}
				else if (expression(i) == '*')
				{
					push(b * a);
				}
				else if (expression(i) == '/')
				{
					push((b / a).toInt);
				}
			}
			i += 1;
		}
		print(expression + " = " + pop() + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyStack = new MyStack();
		//Execute the following expression
		obj.evaluate("12+35+*");
		obj.evaluate("72*62/+4+-");
	}
}

Output

12+35+* = 24
72*62/+4+- = -21
/*
  Swift Program
  Evaluate postfix expression
*/
//Stack Node
class Node
{
	var data: Int;
	var next: Node? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class MyStack
{
	var top: Node? ;
	init()
	{
		self.top = nil;
	}
	//Add a new element in stack
	func push(_ data: Int)
	{
		//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
	//When stack is empty it returns 0
	func pop() -> Int
	{
		var temp: Int = 0;
		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;
		}
	}
	func evaluate(_ exp: String)
	{
		//Get the length of expression
		let size: Int = exp.count;
		var a: Int = 0;
		var b: Int = 0;
		var i: Int = 0;
      	let expression = Array(exp);
		while (i < size)
		{
			if (expression[i] >= "0" && expression[i] <= "9")
			{
				a = Int(UnicodeScalar(String(expression[i]))!.value)  - Int(UnicodeScalar(String("0"))!.value);
				self.push(a);
			}
			else if (!self.is_empty())
			{
				a = self.pop();
				b = self.pop();
				//Perform arithmetic operations 
				if (expression[i] == "+")
				{
					self.push(b + a);
				}
				else if (expression[i] == "-")
				{
					self.push(b - a);
				}
				else if (expression[i] == "*")
				{
					self.push(b * a);
				}
				else if (expression[i] == "/")
				{
					self.push(b / a);
				}
			}
			i += 1;
		}
		print(exp ," = ", self.pop() );
	}
}
func main()
{
	let obj: MyStack = MyStack();
	//Execute the following expression
	obj.evaluate("12+35+*");
	obj.evaluate("72*62/+4+-");
}
main();

Output

12+35+*  =  24
72*62/+4+-  =  -21


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