Insert an element at the bottom of stack

Here given code implementation process.

import java.util.Stack;
/*
    Java Program
    Insert an element at the bottom of stack
    Using recursion
*/
public class MyStack
{
	public void insertAtBottom(Stack < Integer > record, int element)
	{
		if (record.isEmpty() == true)
		{
			// Add new element at the bottom
			record.push(element);
		}
		else
		{
			// Get the top element
			int data = record.peek();
			record.pop();
			// Recursively find deepest element
			insertAtBottom(record, element);
			// Add current data
			record.push(data);
		}
	}
	public static void main(String[] args)
	{
		MyStack task = new MyStack();
		Stack < Integer > record = new Stack < Integer > ();
		// Add initial element
		record.push(10);
		record.push(11);
		record.push(12);
		record.push(15);
		// Add element at bottom
		task.insertAtBottom(record,999);
		// Display stack element
		while (record.isEmpty() == false)
		{
			System.out.println(record.peek());
			record.pop();
		}
	}
}

Output

15
12
11
10
999
// Include header file
#include <iostream>
#include <stack>

using namespace std;
/*
    C++ Program
    Insert an element at the bottom of stack
    Using recursion
*/
class MyStack
{
	public: void insertAtBottom(stack <int> &record, int element)
	{
		if (record.empty() == true)
		{
			// Add new element at the bottom
			record.push(element);
		}
		else
		{
			// Get the top element
			int data = record.top();
			record.pop();
			// Recursively find deepest element
			this->insertAtBottom(record, element);
			// Add current data
			record.push(data);
		}
	}
};
int main()
{
	MyStack *task = new MyStack();
	stack < int > record;
	// Add initial element
	record.push(10);
	record.push(11);
	record.push(12);
	record.push(15);
	// Add element at bottom
	task->insertAtBottom(record, 999);
	// Display stack element
	while (record.empty() == false)
	{
		cout << record.top() << endl;
		record.pop();
	}
	return 0;
}

Output

15
12
11
10
999
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp Program
    Insert an element at the bottom of stack
    Using recursion
*/
public class MyStack
{
	public void insertAtBottom(Stack < int > record, int element)
	{
		if ((record.Count == 0) == true)
		{
			// Add new element at the bottom
			record.Push(element);
		}
		else
		{
			// Get the top element
			int data = record.Peek();
			record.Pop();
			// Recursively find deepest element
			this.insertAtBottom(record, element);
			// Add current data
			record.Push(data);
		}
	}
	public static void Main(String[] args)
	{
		MyStack task = new MyStack();
		Stack < int > record = new Stack < int > ();
		// Add initial element
		record.Push(10);
		record.Push(11);
		record.Push(12);
		record.Push(15);
		// Add element at bottom
		task.insertAtBottom(record, 999);
		// Display stack element
		while ((record.Count == 0) == false)
		{
			Console.WriteLine(record.Peek());
			record.Pop();
		}
	}
}

Output

15
12
11
10
999
<?php
/*
    Php Program
    Insert an element at the bottom of stack
    Using recursion
*/
class MyStack
{
	public	function insertAtBottom(&$record, $element)
	{
		if (empty($record) == true)
		{
			// Add new element at the bottom
			array_push($record, $element);
		}
		else
		{
			// Get the top element
			$data = end($record);
			array_pop($record);
			// Recursively find deepest element
			$this->insertAtBottom($record, $element);
			// Add current data
			array_push($record, $data);
		}
	}
}

function main()
{
	$task = new MyStack();
	$record = array();
	// Add initial element
	array_push($record, 10);
	array_push($record, 11);
	array_push($record, 12);
	array_push($record, 15);
	// Add element at bottom
	$task->insertAtBottom($record, 999);
	// Display stack element
	while (empty($record) == false)
	{
		echo(end($record).
			"\n");
		array_pop($record);
	}
}
main();

Output

15
12
11
10
999
/*
    Node JS Program
    Insert an element at the bottom of stack
    Using recursion
*/
class MyStack
{
	insertAtBottom(record, element)
	{
		if (record.length == 0)
		{
			// Add new element at the bottom
			record.push(element);
		}
		else
		{
			// Get the top element
			var data = record[record.length - 1];
			record.pop();
			// Recursively find deepest element
			this.insertAtBottom(record, element);
			// Add current data
			record.push(data);
		}
	}
}

function main()
{
	var task = new MyStack();
	var record = [];
	// Add initial element
	record.push(10);
	record.push(11);
	record.push(12);
	record.push(15);
	// Add element at bottom
	task.insertAtBottom(record, 999);
	// Display stack element
	while (record.length != 0)
	{
		console.log(record[record.length - 1]);
		record.pop();
	}
}
main();

Output

15
12
11
10
999
#    Python 3 Program
#    Insert an element at the bottom of stack
#    Using recursion
class MyStack :
	def insertAtBottom(self, record, element) :
		if (len(record) == 0) :
			#  Add new element at the bottom
			record.append(element)
		else :
			#  Get the top element
			data = record[-1]
			record.pop()
			#  Recursively find deepest element
			self.insertAtBottom(record, element)
			#  Add current data
			record.append(data)
		
	

def main() :
	task = MyStack()
	record = []
	#  Add initial element
	record.append(10)
	record.append(11)
	record.append(12)
	record.append(15)
	#  Add element at bottom
	task.insertAtBottom(record, 999)
	#  Display stack element
	while (len(record) != 0) :
		print(record[-1])
		record.pop()
	

if __name__ == "__main__": main()

Output

15
12
11
10
999
#    Ruby Program
#    Insert an element at the bottom of stack
#    Using recursion
class MyStack 
	def insertAtBottom(record, element) 
		if (record.length == 0) 
			#  Add new element at the bottom
			record.push(element)
		else
			#  Get the top element
			data = record.last
			record.pop()
			#  Recursively find deepest element
			self.insertAtBottom(record, element)
			#  Add current data
			record.push(data)
		end

	end

end

def main() 
	task = MyStack.new()
	record = []
	#  Add initial element
	record.push(10)
	record.push(11)
	record.push(12)
	record.push(15)
	#  Add element at bottom
	task.insertAtBottom(record, 999)
	#  Display stack element
	while (record.length != 0) 
		print(record.last, "\n")
		record.pop()
	end

end

main()

Output

15
12
11
10
999
import scala.collection.mutable._;
/*
    Scala Program
    Insert an element at the bottom of stack
    Using recursion
*/
class MyStack()
{
	def insertAtBottom(record: Stack[Int], element: Int): Unit = {
		if (record.isEmpty == true)
		{
			// Add new element at the bottom
			record.push(element);
		}
		else
		{
			// Get the top element
			var data: Int = record.top;
			record.pop;
			// Recursively find deepest element
			insertAtBottom(record, element);
			// Add current data
			record.push(data);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: MyStack = new MyStack();
		var record: Stack[Int] = new Stack[Int]();
		// Add initial element
		record.push(10);
		record.push(11);
		record.push(12);
		record.push(15);
		// Add element at bottom
		task.insertAtBottom(record, 999);
		// Display stack element
		while (record.isEmpty == false)
		{
			println(record.top);
			record.pop;
		}
	}
}

Output

15
12
11
10
999
import java.util.Stack;
/*
    Kotlin Program
    Insert an element at the bottom of stack
    Using recursion
*/
class MyStack
{
	fun insertAtBottom(record: Stack < Int > , element : Int): Unit
	{
		if (record.empty() == true)
		{
			// Add new element at the bottom
			record.push(element);
		}
		else
		{
			// Get the top element
			val data: Int = record.peek();
			record.pop();
			// Recursively find deepest element
			this.insertAtBottom(record, element);
			// Add current data
			record.push(data);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: MyStack = MyStack();
	var record: Stack < Int > = Stack < Int > ();
	// Add initial element
	record.push(10);
	record.push(11);
	record.push(12);
	record.push(15);
	// Add element at bottom
	task.insertAtBottom(record, 999);
	// Display stack element
	while (record.empty() == false)
	{
		println(record.peek());
		record.pop();
	}
}

Output

15
12
11
10
999
package main
import "fmt"
/*
    Go Program
    Insert an element at the bottom of stack
*/

func insertAtBottom(record *[] int, element int) {
	
	*record = append([]int{element}, *record...)
}
func main() {
	
	var record = make([] int, 0)
	// Add initial element
	record = append(record, 10)
	record = append(record, 11)
	record = append(record, 12)
	record = append(record, 15)
	// Add element at bottom
	insertAtBottom(&record, 999)
	// Display stack element
	for (len(record) > 0) {
		fmt.Println(record[len(record) - 1])
		record = record[: len(record) - 1]
	}
}

Output

15
12
11
10
999


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