GCD of two numbers using recursion in c++


#include<iostream>
#include<conio.h>
using namespace std;

int gcd(int, int);	// Prototype of recursive function

int main()
{
	int num1;
	int num2;
	int GCF = 1;
	cout << "Enter any two positive integer numbers: " <<endl;
	cin >> num1;
	cin >> num2;
	if(num1 == 0)	/*Check if one number is zero then other one is GCD */
	{
		GCF = num2;
	}
	else if (num2 == 0)
	{
		GCF = num1;
	}
	else		/*Othervise find out the gcd */
	{
		GCF = gcd(num1, num2);
	}
	cout << "\nGreatest common factor of " << num1 << " and " << num2 << " is " << GCF << "." << endl;

	getch();
	return 0;
}

int gcd(int m, int n) 
{
	if(m == n)
		return m;
	else if (m > n)
		return gcd(m-n, n);
	else
		return gcd(m, n-m);
}
Posted in C++ | Tagged , , | Leave a comment

Queue implementation using Linked List


#include<stdio.h>
#include<stdlib.h>

void enQueue();
void deQueue();
void display();

typedef struct node
{
	int data;
	struct node *next;
} qNode;

qNode *front = NULL;
qNode *rear = NULL;

int main(int argc, char *argv[])
{
	int choice;
	do{
		start :
		choice = 0;	/*it will prevent the crashing of application 
				if someone entered an invalid choice like  non integer choice etc.  */

		printf("\n1. Enqueue\n2. Dequeue\n3. Display Queue\n0. Exit" 
			     " \n*****************************\n");
		printf("Enter your choice :  ");
		scanf("%d", &choice);		/* here if you entered any invalid choice then program will be terminate */
		
		switch(choice)
		{
			case 1: enQueue();
				break;
			case 2: deQueue();
				break;
			case 3: display();
				break;
			case 0: exit(1);
			default : 
				printf("\nYou have entered wrong choice try again !\n");
				goto start;
		}
	} while(choice !=0);
	
	return 0;
}

void display()
{
	qNode * temp;
	temp = front;
	if(temp == NULL)
		printf("\nQueue is Empty !\n") ;
	while(temp != NULL)
	{
		printf("%d -> ",temp->data);
		temp = temp->next;
	}

	/*  this * is representing null (i.e there is no more node in Queue !)  */
	if(front != NULL)
		printf("*\n");
}

void enQueue()
{
	qNode *temp;
	int num;
	printf("Enter the integer data : ");
	scanf("%d",&num);
	temp = (qNode*) malloc (sizeof(qNode));
	temp->data = num;
	temp->next = NULL;

	/* if Queue is empty then insert it and make it front and rear */
	if(front == NULL)	
	{
		front = temp;
		rear = temp;
	}
	/*  otherwise insert the element next to rear and make it  rear*/
	else {
		rear->next = temp;
		rear = temp;
	}
	printf("\nQueue is enQueued Successfully !\n");
}


void deQueue()
{
	qNode * temp;
	temp = front;
	if(front != NULL){		/*check whether Queue is Empty or not */
		front = front->next;
		free(temp);
		printf("\nQueue is deQueued Successfully !\n");
	}
	else 
		printf("\nQueue is Empty you can't perform Dequeue operation !\n");
}
Output:queue_list
Posted in C, Data Structure, Linked List | Tagged , , , , , | Leave a comment

Stack implementation using Linked List


#include<stdio.h>
#include<stdlib.h>

//prototypes of used function
void push();
void pop();
void display();

/*structure of stack node  */
typedef struct node
{
	int data;
	struct node *next;
} sNode;

sNode *top = NULL;

/*driver function to test the stack operations */
int main(int argc, char * argv[])
{
	int choice ;

	do{
		start :
		choice =0;	/* it will take care of  if user entered any invalid  choice */
		printf("\nValid Stack Operations \n1. push an item\n2. pop an item \n3. display stack\n0. Exit\n");
		printf("+-------------------------------+\n");
		printf("Enter your choice : ");
		scanf("%d", &choice);	/* Here if your input is invalid program will be terminated simply without crashing */
		
		switch(choice)
		{
			case 1: push();
				break;
			case 2: pop();
				break;
			case 3: display();
				break;
			case 0: exit(1);
				break;
			default :
				printf("\nYou have entered wrong choice, try again !\n");
				goto start;
		}
	}while(choice != 0);

	return 0;	
}

/*function to display the stack status  */
void display()
{
	sNode * temp;
	temp = top;
	printf("\n top -->\n");
	if(temp == NULL)
		printf("\t|_______|\n\t+-Stack-+\n Stack is Empty !\n") ;
	while(temp != NULL)
	{
		printf("\t| %d\t|\n",temp->data);
		temp = temp->next;
	}
	/* it is just for visuality nothing else */
	if(top != NULL)
		printf("\t+-------+\n\t++Stack++\n");

}

/* function to insert the data in to stack */
void push()
{
	int num;
	sNode *temp;

	printf("Enter the data : ");
	scanf("%d", &num);

	/* allocating memory to new stack node*/
	temp = (sNode *) malloc (sizeof(sNode));
	temp->data = num;
	temp->next = NULL;

	/* insert the data at top of stack*/
	if(top == NULL){
		top = temp;
	}
	else{
		temp->next = top;
		top = temp;
	}
	printf("\ndata successfully inserted to stack !\n");
}

/*function to delete the data from stack */
void pop()
{
	sNode * temp;
	temp = top;
	if(temp != NULL){		/*check whether Stack is Empty or not */
		top = top->next;
		free(temp);
		printf("\npop operation successfully completed !\n");
	}
	else 
		printf("\nStack is Empty you can't perform pop operation !\n");
}

Output:stack_list

Posted in C, Data Structure, Linked List | Tagged , , , , , | Leave a comment