Reverse a number using stack

Here given code implementation process.

//C Program
//Reverse a number using stack
#include <stdio.h>

#include <stdlib.h> //for malloc function

//Create structure
struct Stack
{
	int data;
	struct Stack *next;
};
//Add a stack element
void push(int data, struct Stack **top)
{
	struct Stack *new_Stack = (struct Stack *) malloc(sizeof(struct Stack));
	if (new_Stack)
	{
		new_Stack->data = data;
		new_Stack->next = *top;
      	*top = new_Stack;
	}
}
// Remove the top element of stack 
// And return removed element value
int pop(struct Stack **top)
{
	if ( *top != NULL)
	{
		struct Stack *auxiliary = *top;
		int data = auxiliary->data;
      	*top = auxiliary->next;
		free(auxiliary);
		auxiliary = NULL;
		return data;
	}
	return 0;
}
//Reverse the given number
int reverse(int number)
{
	printf("Before %d ", number);
	struct Stack *top = NULL;
	while (number != 0)
	{
		//Add last digit in top of the stack
		push(number % 10, & top);
		//remove last digit
		number /= 10;
	}
	int multiply = 1;
	while (top != NULL)
	{
		//Get stack top element and combine into actual number
		number = pop( & top) *multiply + number;
		multiply *= 10;
	}
	return number;
}
int main()
{
	//Test Case
	//When have negative number 
	int number = -381725;
	number = reverse(number);
	printf("After %d\n", number);
	number = 62514;
	number = reverse(number);
	printf("After %d\n", number);
	//When number containing zero at the end
	//In this case not take zero at beginning of number
	number = 12340;
	number = reverse(number);
	printf("After %d\n", number);
	return 0;
}

Output

Before -381725 After -527183
Before 62514 After 41526
Before 12340 After 4321
/*
  Java Program
  Reverse a number using stack
*/
//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 0
	public int pop()
	{
		int temp = 0;
		if (top != null)
		{
			temp = top.data;
			top = top.next;
		}
		return temp;
	}
	//Reverse the given number
	public int reverse(int number)
	{
		System.out.print("Before " + number + " ");
		while (number != 0)
		{
			//Add last digit in top of the stack
			this.push(number % 10);
			//remove last digit
			number /= 10;
		}
		int multiply = 1;
		while (top != null)
		{
			//Get stack top element and combine into actual number
			number = this.pop() * multiply + number;
			multiply *= 10;
		}
		return number;
	}
	public static void main(String[] args)
	{
		MyStack obj = new MyStack();
		//Test Case
		//When have negative number 
		int number = -381725;
		number = obj.reverse(number);
		System.out.print("After " + number + " \n");
		number = 62514;
		number = obj.reverse(number);
		System.out.print("After " + number + " \n");
		//When number containing zero at the end
		//In this case not take zero at beginning of number
		number = 12340;
		number = obj.reverse(number);
		System.out.print("After " + number + " \n");
	}
}

Output

Before -381725 After -527183
Before 62514 After 41526
Before 12340 After 4321
/*
  C++ Program
  Reverse a number using stack
*/
//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 0
	int pop()
	{
		int temp = 0;
		if (this->top != NULL)
		{	Node *element = top;
			temp = this->top->data;
			this->top = this->top->next;
         	delete element;
		}
		return temp;
	}
	//Reverse the given number
	int reverse(int number)
	{
		cout << "Before " << number << " ";
		while (number != 0)
		{
			//Add last digit in top of the stack
			this->push(number % 10);
			//remove last digit
			number /= 10;
		}
		int multiply = 1;
		while (this->top != NULL)
		{
			//Get stack top element and combine into actual number
			number = this->pop() * multiply + number;
			multiply *= 10;
		}
		return number;
	}
};
int main()
{
	MyStack obj = MyStack();
	//Test Case
	//When have negative number 
	int number = -381725;
	number = obj.reverse(number);
	cout << "After " << number << " \n";
	number = 62514;
	number = obj.reverse(number);
	cout << "After " << number << " \n";
	//When number containing zero at the end
	//In this case not take zero at beginning of number
	number = 12340;
	number = obj.reverse(number);
	cout << "After " << number << " \n";
	return 0;
}

Output

Before -381725 After -527183
Before 62514 After 41526
Before 12340 After 4321
/*
  C# Program
  Reverse a number using stack
*/
//Stack Node
using System;
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
		{
			Console.Write("Memory overflow\n");
		}
	}
	//Add a top element in stack
	//When stack is empty it returns 0
	public int pop()
	{
		int temp = 0;
		if (top != null)
		{
			temp = top.data;
			top = top.next;
		}
		return temp;
	}
	//Reverse the given number
	public int reverse(int number)
	{
		Console.Write("Before " + number + " ");
		while (number != 0)
		{
			//Add last digit in top of the stack
			this.push(number % 10);
			//remove last digit
			number /= 10;
		}
		int multiply = 1;
		while (top != null)
		{
			//Get stack top element and combine into actual number
			number = this.pop() * multiply + number;
			multiply *= 10;
		}
		return number;
	}
	public static void Main(String[] args)
	{
		MyStack obj = new MyStack();
		//Test Case
		//When have negative number 
		int number = -381725;
		number = obj.reverse(number);
		Console.Write("After " + number + " \n");
		number = 62514;
		number = obj.reverse(number);
		Console.Write("After " + number + " \n");
		//When number containing zero at the end
		//In this case not take zero at beginning of number
		number = 12340;
		number = obj.reverse(number);
		Console.Write("After " + number + " \n");
	}
}

Output

Before -381725 After -527183
Before 62514 After 41526
Before 12340 After 4321
<?php
/*
  Php Program
  Reverse a number using stack
*/
//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 0
	public 	function pop()
	{
		$temp = 0;
		if ($this->top != null)
		{
			$temp = $this->top->data;
			$this->top = $this->top->next;
		}
		return $temp;
	}
	//Reverse the given number
	public 	function reverse($number)
	{
		echo("Before ". $number ." ");
		while ($number != 0)
		{
			//Add last digit in top of the stack
			$this->push($number % 10);
			$number = intval($number /
				//remove last digit
				10);
		}
		$multiply = 1;
		while ($this->top != null)
		{
			//Get stack top element and combine into actual number
			$number = $this->pop() * $multiply + $number;
			$multiply *= 10;
		}
		return $number;
	}
}

function main()
{
	$obj = new MyStack();
	//Test Case
	//When have negative number 
	$number = -381725;
	$number = $obj->reverse($number);
	echo("After ". $number ." \n");
	$number = 62514;
	$number = $obj->reverse($number);
	echo("After ". $number ." \n");
	//When number containing zero at the end
	//In this case not take zero at beginning of number
	$number = 12340;
	$number = $obj->reverse($number);
	echo("After ". $number ." \n");
}
main();

Output

Before -381725 After -527183
Before 62514 After 41526
Before 12340 After 4321
/*
  Node Js Program
  Reverse a number using stack
*/
//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 0
	pop()
	{
		var temp = 0;
		if (this.top != null)
		{
			temp = this.top.data;
			this.top = this.top.next;
		}
		return temp;
	}
	//Reverse the given number
	reverse(number)
	{
		process.stdout.write("Before " + number + " ");
		while (number != 0)
		{
			//Add last digit in top of the stack
			this.push(number % 10);
			number = parseInt(number /
				//remove last digit
				10);
		}
		var multiply = 1;
		while (this.top != null)
		{
			//Get stack top element and combine into actual number
			number = this.pop() * multiply + number;
			multiply *= 10;
		}
		return number;
	}
}

function main(args)
{
	var obj = new MyStack();
	//Test Case
	//When have negative number 
	var number = -381725;
	number = obj.reverse(number);
	process.stdout.write("After " + number + " \n");
	number = 62514;
	number = obj.reverse(number);
	process.stdout.write("After " + number + " \n");
	//When number containing zero at the end
	//In this case not take zero at beginning of number
	number = 12340;
	number = obj.reverse(number);
	process.stdout.write("After " + number + " \n");
}
main();

Output

Before -381725 After -527183
Before 62514 After 41526
Before 12340 After 4321
#   Python 3 Program
#   Reverse a number using stack

# 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
	
	# Reverse the given number
	def reverse(self, number) :
		print("Before ", number , end = "  ")
		while (number != 0) :
			# Add last digit in top of the stack
			self.push(number % 10)
			# remove last digit
			number = int(number / 10)
		
		multiply = 1
		while (self.top != None) :
			# Get stack top element and combine into actual number
			number = self.pop() * multiply + number
			multiply *= 10
		
		return number
	

def main() :
	obj = MyStack()
	# Test Case
	# When have negative number 
	number = -381725
	number = obj.reverse(number)
	print("After ", number )
	number = 62514
	number = obj.reverse(number)
	print("After ", number )
	# When number containing zero at the end
	# In this case not take zero at beginning of number
	number = 12340
	number = obj.reverse(number)
	print("After ", number )


if __name__ == "__main__": main()

Output

Before  -381725  After  583927
Before  62514  After  41526
Before  12340  After  4321
#   Ruby Program
#   Reverse a number using stack

# 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
	# Reverse the given number
	def reverse(number)
	
		print("Before ", number ," ")
		flag = false
		if (number < 0)
		
			number = -number
			flag = true
		end
		while (number != 0)
		
			# Add last digit in top of the stack
			self.push(number % 10)
			# remove last digit
			number /= 10
		end
		multiply = 1
		while (@top != nil)
		
			# Get stack top element and combine into actual number
			number = self.pop() * multiply + number
			multiply *= 10
		end
		if (flag == true)
		
			number = -number
		end
		return number
	end
end
def main()

	obj = MyStack.new()
	# Test Case
	# When have negative number 
	number = -381725
	number = obj.reverse(number)
	print("After ", number ," \n")
	number = 62514
	number = obj.reverse(number)
	print("After ", number ," \n")
	# When number containing zero at the end
	# In this case not take zero at beginning of number
	number = 12340
	number = obj.reverse(number)
	print("After ", number ," \n")
end
main()

Output

Before -381725 After -527183 
Before 62514 After 41526 
Before 12340 After 4321 
/*
  Scala Program
  Reverse a number using stack
*/
//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;
	}
	//Reverse the given number
	def reverse(num: Int): Int = {
      	var number = num;
		print("Before " + number + " ");
		var flag: Boolean = false;
		if (number < 0)
		{
			number = -number;
			flag = true;
		}
		while (number != 0)
		{
			//Add last digit in top of the stack
			this.push(number % 10);
          	//remove last digit
			number = (number /10).toInt;
		}
		var multiply: Int = 1;
		while (top != null)
		{
			//Get stack top element and combine into actual number
			number = this.pop() * multiply + number;
			multiply *= 10;
		}
		if (flag == true)
		{
			number = -number;
		}
		return number;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyStack = new MyStack();
		//Test Case
		//When have negative number 
		var number: Int = -381725;
		number = obj.reverse(number);
		print("After " + number + " \n");
		number = 62514;
		number = obj.reverse(number);
		print("After " + number + " \n");
		//When number containing zero at the end
		//In this case not take zero at beginning of number
		number = 12340;
		number = obj.reverse(number);
		print("After " + number + " \n");
	}
}

Output

Before -381725 After -527183
Before 62514 After 41526
Before 12340 After 4321
/*
  Swift Program
  Reverse a number using stack
*/
//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;
	}
	//Reverse the given number
	func reverse(_ num: Int) -> Int
	{
      	var number: Int = num;
		print("Before ", number ,terminator: "  ");
		var flag: Bool = false;
		if (number < 0)
		{
			number = -number;
			flag = true;
		}
		while (number != 0)
		{
			//Add last digit in top of the stack
			self.push(number % 10);
			//remove last digit
			number /= 10;
		}
		var multiply: Int = 1;
		while (self.top != nil)
		{
			//Get stack top element and combine into actual number
			number = self.pop() * multiply + number;
			multiply *= 10;
		}
		if (flag == true)
		{
			number = -number;
		}
		return number;
	}
}
func main()
{
	let obj: MyStack = MyStack();
	//Test Case
	//When have negative number 
	var number: Int = -381725;
	number = obj.reverse(number);
	print("After ", number );
	number = 62514;
	number = obj.reverse(number);
	print("After ", number );
	//When number containing zero at the end
	//In this case not take zero at beginning of number
	number = 12340;
	number = obj.reverse(number);
	print("After ", number );
}
main();

Output

Before  -381725  After  -527183
Before  62514  After  41526
Before  12340  After  4321

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