Generate all initial n numbers formed from 3 and 4

Here given code implementation process.

// Java Program 
// Generate all initial n numbers formed from 3 and 4
class QNode
{
	public String data;
	public QNode next;
	public QNode(String data)
	{
		this.data = data;
		this.next = null;
	}
}
// Define custom queue class
class MyQueue
{
	public QNode front;
	public QNode rear;
	public int size;
	public MyQueue()
	{
		this.front = null;
		this.rear = null;
		this.size = 0;
	}
	// Add a new node at last of queue
	public void enqueue(String data)
	{
		QNode node = new QNode(data);
		if (this.front == null)
		{
			// When first node of queue
			this.front = node;
		}
		else
		{
			// Add node at last level
			this.rear.next = node;
		}
		this.size++;
		this.rear = node;
	}
	// Delete front node of queue
	public void dequeue()
	{
		if (this.front != null)
		{
			if (this.rear == this.front)
			{
				this.rear = null;
				this.front = null;
			}
			else
			{
				this.front = this.front.next;
			}
			this.size--;
		}
	}
	public int isSize()
	{
		return this.size;
	}
	public boolean isEmpty()
	{
		if (this.isSize() == 0)
		{
			return true;
		}
		return false;
	}
	public QNode peek()
	{
		if (this.isSize() == 0)
		{
			return null;
		}
		else
		{
			return this.front;
		}
	}
}
public class NumberCombination
{
	public void combination3And4(int n)
	{
		if (n <= 0)
		{
			return;
		}
		if (n == 1)
		{
			System.out.println("1");
			return;
		}
		MyQueue record = new MyQueue();
		QNode temp = null;
		// first number
		record.enqueue("3");
		// second number
		record.enqueue("4");
		int count = 2;
		while (record.isEmpty() == false)
		{
			// Get front node
			temp = record.peek();
			if (count < n)
			{
				record.enqueue(temp.data + "3");
				count++;
			}
			if (count < n)
			{
				record.enqueue(temp.data + "4");
				count++;
			}
			System.out.println(temp.data);
			// Remove front node
			record.dequeue();
		}
	}
	public static void main(String args[])
	{
		NumberCombination task = new NumberCombination();
		// Test n = 20
		task.combination3And4(20);
	}
}

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434
// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Generate all initial n numbers formed from 3 and 4
class QNode
{
	public: string data;
	QNode *next;
	QNode(string data)
	{
		this->data = data;
		this->next = NULL;
	}
};
// Define custom queue class
class MyQueue
{
	public: 
    QNode *front;
	QNode *rear;
	int size;
	MyQueue()
	{
		this->front = NULL;
		this->rear = NULL;
		this->size = 0;
	}
	// Add a new node at last of queue
	void enqueue(string data)
	{
		QNode *node = new QNode(data);
		if (this->front == NULL)
		{
			// When first node of queue
			this->front = node;
		}
		else
		{
			// Add node at last level
			this->rear->next = node;
		}
		this->size++;
		this->rear = node;
	}
	// Delete front node of queue
	void dequeue()
	{
		if (this->front != NULL)
		{
			if (this->rear == this->front)
			{
				this->rear = NULL;
				this->front = NULL;
			}
			else
			{
				this->front = this->front->next;
			}
			this->size--;
		}
	}
	int isSize()
	{
		return this->size;
	}
	bool isEmpty()
	{
		if (this->isSize() == 0)
		{
			return true;
		}
		return false;
	}
	QNode *peek()
	{
		if (this->isSize() == 0)
		{
			return NULL;
		}
		else
		{
			return this->front;
		}
	}
};
class NumberCombination
{
	public: void combination3And4(int n)
	{
		if (n <= 0)
		{
			return;
		}
		if (n == 1)
		{
			cout << "1" << endl;
			return;
		}
		MyQueue *record = new MyQueue();
		QNode *temp = NULL;
		// first number
		record->enqueue("3");
		// second number
		record->enqueue("4");
		int count = 2;
		while (record->isEmpty() == false)
		{
			// Get front node
			temp = record->peek();
			if (count < n)
			{
				record->enqueue(temp->data  +  "3");
				count++;
			}
			if (count < n)
			{
				record->enqueue(temp->data  +  "4");
				count++;
			}
			cout << temp->data << endl;
			// Remove front node
			record->dequeue();
		}
	}
};
int main()
{
	NumberCombination *task = new NumberCombination();
	// Test n = 20
	task->combination3And4(20);
	return 0;
}

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434
// Include namespace system
using System;
// Csharp Program
// Generate all initial n numbers formed from 3 and 4
public class QNode
{
	public String data;
	public QNode next;
	public QNode(String data)
	{
		this.data = data;
		this.next = null;
	}
}
// Define custom queue class
public class MyQueue
{
	public QNode front;
	public QNode rear;
	public int size;
	public MyQueue()
	{
		this.front = null;
		this.rear = null;
		this.size = 0;
	}
	// Add a new node at last of queue
	public void enqueue(String data)
	{
		QNode node = new QNode(data);
		if (this.front == null)
		{
			// When first node of queue
			this.front = node;
		}
		else
		{
			// Add node at last level
			this.rear.next = node;
		}
		this.size++;
		this.rear = node;
	}
	// Delete front node of queue
	public void dequeue()
	{
		if (this.front != null)
		{
			if (this.rear == this.front)
			{
				this.rear = null;
				this.front = null;
			}
			else
			{
				this.front = this.front.next;
			}
			this.size--;
		}
	}
	public int isSize()
	{
		return this.size;
	}
	public Boolean isEmpty()
	{
		if (this.isSize() == 0)
		{
			return true;
		}
		return false;
	}
	public QNode peek()
	{
		if (this.isSize() == 0)
		{
			return null;
		}
		else
		{
			return this.front;
		}
	}
}
public class NumberCombination
{
	public void combination3And4(int n)
	{
		if (n <= 0)
		{
			return;
		}
		if (n == 1)
		{
			Console.WriteLine("1");
			return;
		}
		MyQueue record = new MyQueue();
		QNode temp = null;
		// first number
		record.enqueue("3");
		// second number
		record.enqueue("4");
		int count = 2;
		while (record.isEmpty() == false)
		{
			// Get front node
			temp = record.peek();
			if (count < n)
			{
				record.enqueue(temp.data + "3");
				count++;
			}
			if (count < n)
			{
				record.enqueue(temp.data + "4");
				count++;
			}
			Console.WriteLine(temp.data);
			// Remove front node
			record.dequeue();
		}
	}
	public static void Main(String[] args)
	{
		NumberCombination task = new NumberCombination();
		// Test n = 20
		task.combination3And4(20);
	}
}

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434
package main
import "fmt"
// Go Program
// Generate all initial n numbers formed from 3 and 4
type QNode struct {
	data string
	next * QNode
}
func getQNode(data string) * QNode {
	var me *QNode = &QNode {}
	me.data = data
	me.next = nil
	return me
}
// Define custom queue class
type MyQueue struct {
	front * QNode
	rear * QNode
	size int
}
func getMyQueue() * MyQueue {
	var me *MyQueue = &MyQueue {}
	me.front = nil
	me.rear = nil
	me.size = 0
	return me
}
// Add a new node at last of queue
func(this *MyQueue) enqueue(data string) {
	var node * QNode = getQNode(data)
	if this.front == nil {
		// When first node of queue
		this.front = node
	} else {
		// Add node at last level
		this.rear.next = node
	}
	this.size++
	this.rear = node
}
// Delete front node of queue
func(this *MyQueue) dequeue() {
	if this.front != nil {
		if this.rear == this.front {
			this.rear = nil
			this.front = nil
		} else {
			this.front = this.front.next
		}
		this.size--
	}
}
func(this MyQueue) isSize() int {
	return this.size
}
func(this MyQueue) isEmpty() bool {
	if this.isSize() == 0 {
		return true
	}
	return false
}
func(this MyQueue) peek() * QNode {
	if this.isSize() == 0 {
		return nil
	} else {
		return this.front
	}
}
type NumberCombination struct {}
func getNumberCombination() * NumberCombination {
	var me *NumberCombination = &NumberCombination {}
	return me
}
func(this NumberCombination) combination3And4(n int) {
	if n <= 0 {
		return
	}
	if n == 1 {
		fmt.Println("1")
		return
	}
	var record * MyQueue = getMyQueue()
	var temp * QNode = nil
	// first number
	record.enqueue("3")
	// second number
	record.enqueue("4")
	var count int = 2
	for (record.isEmpty() == false) {
		// Get front node
		temp = record.peek()
		if count < n {
			record.enqueue(temp.data + "3")
			count++
		}
		if count < n {
			record.enqueue(temp.data + "4")
			count++
		}
		fmt.Println(temp.data)
		// Remove front node
		record.dequeue()
	}
}
func main() {
	var task * NumberCombination = getNumberCombination()
	// Test n = 20
	task.combination3And4(20)
}

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434
<?php
// Php Program
// Generate all initial n numbers formed from 3 and 4
class QNode
{
	public $data;
	public $next;
	public	function __construct($data)
	{
		$this->data = $data;
		$this->next = NULL;
	}
}
// Define custom queue class
class MyQueue
{
	public $front;
	public $rear;
	public $size;
	public	function __construct()
	{
		$this->front = NULL;
		$this->rear = NULL;
		$this->size = 0;
	}
	// Add a new node at last of queue
	public	function enqueue($data)
	{
		$node = new QNode($data);
		if ($this->front == NULL)
		{
			// When first node of queue
			$this->front = $node;
		}
		else
		{
			// Add node at last level
			$this->rear->next = $node;
		}
		$this->size++;
		$this->rear = $node;
	}
	// Delete front node of queue
	public	function dequeue()
	{
		if ($this->front != NULL)
		{
			if ($this->rear == $this->front)
			{
				$this->rear = NULL;
				$this->front = NULL;
			}
			else
			{
				$this->front = $this->front->next;
			}
			$this->size--;
		}
	}
	public	function isSize()
	{
		return $this->size;
	}
	public	function isEmpty()
	{
		if ($this->isSize() == 0)
		{
			return true;
		}
		return false;
	}
	public	function peek()
	{
		if ($this->isSize() == 0)
		{
			return NULL;
		}
		else
		{
			return $this->front;
		}
	}
}
class NumberCombination
{
	public	function combination3And4($n)
	{
		if ($n <= 0)
		{
			return;
		}
		if ($n == 1)
		{
			echo("1\n");
			return;
		}
		$record = new MyQueue();
		$temp = NULL;
		// first number
		$record->enqueue("3");
		// second number
		$record->enqueue("4");
		$count = 2;
		while ($record->isEmpty() == false)
		{
			// Get front node
			$temp = $record->peek();
			if ($count < $n)
			{
				$record->enqueue($temp->data."3");
				$count++;
			}
			if ($count < $n)
			{
				$record->enqueue($temp->data."4");
				$count++;
			}
			echo($temp->data."\n");
			// Remove front node
			$record->dequeue();
		}
	}
}

function main()
{
	$task = new NumberCombination();
	// Test n = 20
	$task->combination3And4(20);
}
main();

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434
// Node JS Program
// Generate all initial n numbers formed from 3 and 4
class QNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
// Define custom queue class
class MyQueue
{
	constructor()
	{
		this.front = null;
		this.rear = null;
		this.size = 0;
	}
	// Add a new node at last of queue
	enqueue(data)
	{
		var node = new QNode(data);
		if (this.front == null)
		{
			// When first node of queue
			this.front = node;
		}
		else
		{
			// Add node at last level
			this.rear.next = node;
		}
		this.size++;
		this.rear = node;
	}
	// Delete front node of queue
	dequeue()
	{
		if (this.front != null)
		{
			if (this.rear == this.front)
			{
				this.rear = null;
				this.front = null;
			}
			else
			{
				this.front = this.front.next;
			}
			this.size--;
		}
	}
	isSize()
	{
		return this.size;
	}
	isEmpty()
	{
		if (this.isSize() == 0)
		{
			return true;
		}
		return false;
	}
	peek()
	{
		if (this.isSize() == 0)
		{
			return null;
		}
		else
		{
			return this.front;
		}
	}
}
class NumberCombination
{
	combination3And4(n)
	{
		if (n <= 0)
		{
			return;
		}
		if (n == 1)
		{
			console.log("1");
			return;
		}
		var record = new MyQueue();
		var temp = null;
		// first number
		record.enqueue("3");
		// second number
		record.enqueue("4");
		var count = 2;
		while (record.isEmpty() == false)
		{
			// Get front node
			temp = record.peek();
			if (count < n)
			{
				record.enqueue(temp.data + "3");
				count++;
			}
			if (count < n)
			{
				record.enqueue(temp.data + "4");
				count++;
			}
			console.log(temp.data);
			// Remove front node
			record.dequeue();
		}
	}
}

function main()
{
	var task = new NumberCombination();
	// Test n = 20
	task.combination3And4(20);
}
main();

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434
#  Python 3 Program
#  Generate all initial n numbers formed from 3 and 4
class QNode :
	def __init__(self, data) :
		self.data = data
		self.next = None
	

#  Define custom queue class
class MyQueue :
	def __init__(self) :
		self.front = None
		self.rear = None
		self.size = 0
	
	#  Add a new node at last of queue
	def enqueue(self, data) :
		node = QNode(data)
		if (self.front == None) :
			#  When first node of queue
			self.front = node
		else :
			#  Add node at last level
			self.rear.next = node
		
		self.size += 1
		self.rear = node
	
	#  Delete front node of queue
	def dequeue(self) :
		if (self.front != None) :
			if (self.rear == self.front) :
				self.rear = None
				self.front = None
			else :
				self.front = self.front.next
			
			self.size -= 1
		
	
	def isSize(self) :
		return self.size
	
	def isEmpty(self) :
		if (self.isSize() == 0) :
			return True
		
		return False
	
	def peek(self) :
		if (self.isSize() == 0) :
			return None
		else :
			return self.front
		
	

class NumberCombination :
	def combination3And4(self, n) :
		if (n <= 0) :
			return
		
		if (n == 1) :
			print("1")
			return
		
		record = MyQueue()
		temp = None
		#  first number
		record.enqueue("3")
		#  second number
		record.enqueue("4")
		count = 2
		while (record.isEmpty() == False) :
			#  Get front node
			temp = record.peek()
			if (count < n) :
				record.enqueue(temp.data + "3")
				count += 1
			
			if (count < n) :
				record.enqueue(temp.data + "4")
				count += 1
			
			print(temp.data)
			#  Remove front node
			record.dequeue()
		
	

def main() :
	task = NumberCombination()
	#  Test n = 20
	task.combination3And4(20)

if __name__ == "__main__": main()

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434
#  Ruby Program
#  Generate all initial n numbers formed from 3 and 4
class QNode 
	# Define the accessor and reader of class QNode
	attr_reader :data, :next
	attr_accessor :data, :next
	def initialize(data) 
		self.data = data
		self.next = nil
	end

end

#  Define custom queue class
class MyQueue 
	# Define the accessor and reader of class MyQueue
	attr_reader :front, :rear, :size
	attr_accessor :front, :rear, :size
	def initialize() 
		self.front = nil
		self.rear = nil
		self.size = 0
	end

	#  Add a new node at last of queue
	def enqueue(data) 
		node = QNode.new(data)
		if (self.front == nil) 
			#  When first node of queue
			self.front = node
		else
 
			#  Add node at last level
			self.rear.next = node
		end

		self.size += 1
		self.rear = node
	end

	#  Delete front node of queue
	def dequeue() 
		if (self.front != nil) 
			if (self.rear == self.front) 
				self.rear = nil
				self.front = nil
			else
 
				self.front = self.front.next
			end

			self.size -= 1
		end

	end

	def isSize() 
		return self.size
	end

	def isEmpty() 
		if (self.isSize() == 0) 
			return true
		end

		return false
	end

	def peek() 
		if (self.isSize() == 0) 
			return nil
		else
 
			return self.front
		end

	end

end

class NumberCombination 
	def combination3And4(n) 
		if (n <= 0) 
			return
		end

		if (n == 1) 
			print("1", "\n")
			return
		end

		record = MyQueue.new()
		temp = nil
		#  first number
		record.enqueue("3")
		#  second number
		record.enqueue("4")
		count = 2
		while (record.isEmpty() == false) 
			#  Get front node
			temp = record.peek()
			if (count < n) 
				record.enqueue(temp.data + "3")
				count += 1
			end

			if (count < n) 
				record.enqueue(temp.data + "4")
				count += 1
			end

			print(temp.data, "\n")
			#  Remove front node
			record.dequeue()
		end

	end

end

def main() 
	task = NumberCombination.new()
	#  Test n = 20
	task.combination3And4(20)
end

main()

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434
// Scala Program
// Generate all initial n numbers formed from 3 and 4
class QNode(var data: String,
	var next: QNode)
{
	def this(data: String)
	{
		this(data, null)
	}
}
// Define custom queue class
class MyQueue(var front: QNode,
	var rear: QNode,
		var size: Int)
{
	def this()
	{
		this(null, null, 0)
	}
	// Add a new node at last of queue
	def enqueue(data: String): Unit = {
		var node: QNode = new QNode(data);
		if (this.front == null)
		{
			// When first node of queue
			this.front = node;
		}
		else
		{
			// Add node at last level
			this.rear.next = node;
		}
		this.size += 1;
		this.rear = node;
	}
	// Delete front node of queue
	def dequeue(): Unit = {
		if (this.front != null)
		{
			if (this.rear == this.front)
			{
				this.rear = null;
				this.front = null;
			}
			else
			{
				this.front = this.front.next;
			}
			this.size -= 1;
		}
	}
	def isSize(): Int = {
		return this.size;
	}
	def isEmpty(): Boolean = {
		if (this.isSize() == 0)
		{
			return true;
		}
		return false;
	}
	def peek(): QNode = {
		if (this.isSize() == 0)
		{
			return null;
		}
		else
		{
			return this.front;
		}
	}
}
class NumberCombination()
{
	def combination3And4(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		if (n == 1)
		{
			println("1");
			return;
		}
		var record: MyQueue = new MyQueue();
		var temp: QNode = null;
		// first number
		record.enqueue("3");
		// second number
		record.enqueue("4");
		var count: Int = 2;
		while (record.isEmpty() == false)
		{
			// Get front node
			temp = record.peek();
			if (count < n)
			{
				record.enqueue(temp.data + "3");
				count += 1;
			}
			if (count < n)
			{
				record.enqueue(temp.data + "4");
				count += 1;
			}
			println(temp.data);
			// Remove front node
			record.dequeue();
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: NumberCombination = new NumberCombination();
		// Test n = 20
		task.combination3And4(20);
	}
}

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434
// Swift 4 Program
// Generate all initial n numbers formed from 3 and 4
class QNode
{
	var data: String;
	var next: QNode? ;
	init(_ data: String)
	{
		self.data = data;
		self.next = nil;
	}
}
// Define custom queue class
class MyQueue
{
	var front: QNode? ;
	var rear: QNode? ;
	var size: Int;
	init()
	{
		self.front = nil;
		self.rear = nil;
		self.size = 0;
	}
	// Add a new node at last of queue
	func enqueue(_ data: String)
	{
		let node: QNode = QNode(data);
		if (self.front == nil)
		{
			// When first node of queue
			self.front = node;
		}
		else
		{
			// Add node at last level
			self.rear!.next = node;
		}
		self.size += 1;
		self.rear = node;
	}
	// Delete front node of queue
	func dequeue()
	{
		if (self.front  != nil)
		{
			if (self.rear === self.front)
			{
				self.rear = nil;
				self.front = nil;
			}
			else
			{
				self.front = self.front!.next;
			}
			self.size -= 1;
		}
	}
	func isSize() -> Int
	{
		return self.size;
	}
	func isEmpty() -> Bool
	{
		if (self.isSize() == 0)
		{
			return true;
		}
		return false;
	}
	func peek() -> QNode?
	{
		if (self.isSize() == 0)
		{
			return nil;
		}
		else
		{
			return self.front;
		}
	}
}
class NumberCombination
{
	func combination3And4(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		if (n == 1)
		{
			print("1");
			return;
		}
		let record: MyQueue = MyQueue();
		var temp: QNode? = nil;
		// first number
		record.enqueue("3");
		// second number
		record.enqueue("4");
		var count: Int = 2;
		while (record.isEmpty() == false)
		{
			// Get front node
			temp = record.peek();
			if (count < n)
			{
				record.enqueue(temp!.data + "3");
				count += 1;
			}
			if (count < n)
			{
				record.enqueue(temp!.data + "4");
				count += 1;
			}
			print(temp!.data);
			// Remove front node
			record.dequeue();
		}
	}
}
func main()
{
	let task: NumberCombination = NumberCombination();
	// Test n = 20
	task.combination3And4(20);
}
main();

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434
// Kotlin Program
// Generate all initial n numbers formed from 3 and 4
class QNode
{
	var data: String;
	var next: QNode ? ;
	constructor(data: String)
	{
		this.data = data;
		this.next = null;
	}
}
// Define custom queue class
class MyQueue
{
	var front: QNode ? ;
	var rear: QNode ? ;
	var size: Int;
	constructor()
	{
		this.front = null;
		this.rear = null;
		this.size = 0;
	}
	// Add a new node at last of queue
	fun enqueue(data: String): Unit
	{
		val node: QNode = QNode(data);
		if (this.front == null)
		{
			// When first node of queue
			this.front = node;
		}
		else
		{
			// Add node at last level
			this.rear?.next = node;
		}
		this.size += 1;
		this.rear = node;
	}
	// Delete front node of queue
	fun dequeue(): Unit
	{
		if (this.front != null)
		{
			if (this.rear == this.front)
			{
				this.rear = null;
				this.front = null;
			}
			else
			{
				this.front = this.front?.next;
			}
			this.size -= 1;
		}
	}
	fun isSize(): Int
	{
		return this.size;
	}
	fun isEmpty(): Boolean
	{
		if (this.isSize() == 0)
		{
			return true;
		}
		return false;
	}
	fun peek(): QNode ?
	{
		if (this.isSize() == 0)
		{
			return null;
		}
		else
		{
			return this.front;
		}
	}
}
class NumberCombination
{
	fun combination3And4(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		if (n == 1)
		{
			println("1");
			return;
		}
		val record: MyQueue = MyQueue();
		var temp: QNode ? ;
		// first number
		record.enqueue("3");
		// second number
		record.enqueue("4");
		var count: Int = 2;
		while (record.isEmpty() == false)
		{
			// Get front node
			temp = record.peek();
			if (count < n)
			{
				record.enqueue(temp?.data + "3");
				count += 1;
			}
			if (count < n)
			{
				record.enqueue(temp?.data + "4");
				count += 1;
			}
			println(temp?.data);
			// Remove front node
			record.dequeue();
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: NumberCombination = NumberCombination();
	// Test n = 20
	task.combination3And4(20);
}

Output

3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
3334
3343
3344
3433
3434


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