Reverse a Stack using Recursion
#include
#include
#include
struct node
{
int a;
struct node *next;
};
void generate(struct node **);
void display(struct node *);
void stack_reverse(struct node **, struct node **);
void delete(struct node **);
int main()
{
struct node *head = NULL;
generate(&head);
printf("\nThe sequence of contents in stack\n");
display(head);
printf("\nInversing the contents of the stack\n");
if (head != NULL)
{
stack_reverse(&head, &(head->next));
}
printf("\nThe contents in stack after reversal\n");
display(head);
delete(&head);
return 0;
}
void stack_reverse(struct node **head, struct node **head_next)
{
struct node *temp;
if (*head_next != NULL)
{
temp = (*head_next)->next;
(*head_next)->next = (*head);
*head = *head_next;
*head_next = temp;
stack_reverse(head, head_next);
}
}
void display(struct node *head)
{
if (head != NULL)
{
printf("%d ", head->a);
display(head->next);
}
}
void generate(struct node **head)
{
int num, i;
struct node *temp;
printf("Enter length of list: ");
scanf("%d", &num);
for (i = num; i > 0; i--)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
if (*head == NULL)
{
*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}
void delete(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
Output
Enter length of list: 10
The sequence of contents in stack
1 2 3 4 5 6 7 8 9 10
Inversing the contents of the stack
The contents in stack after reversal
10 9 8 7 6 5 4 3 2 1
Implement Selection Sort Recursively
#include
#include
void selection(int [], int, int, int, int);
int main()
{
int list[30], size, temp, i, j;
printf("Enter the size of the list: ");
scanf("%d", &size);
printf("Enter the elements in list:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
selection(list, 0, 0, size, 1);
printf("The sorted list in ascending order is\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
return 0;
}
void selection(int list[], int i, int j, int size, int flag)
{
int temp;
if (i < size - 1)
{
if (flag)
{
j = i + 1;
}
if (j < size)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
selection(list, i, j + 1, size, 0);
}
selection(list, i + 1, 0, size, 1);
}
}
Output
Enter the size of the list: 5
Enter the elements in list:
23
45
64
12
34
The sorted list in ascending order is
12 23 34 45 64
Perform Matrix Multiplication using Recursion
#include
#include
void multiply(int, int, int [][10], int, int, int [][10], int [][10]);
void display(int, int, int[][10]);
int main()
{
int a[10][10], b[10][10], c[10][10] = {0};
int m1, n1, m2, n2, i, j, k;
printf("Enter rows and columns for Matrix A respectively: ");
scanf("%d%d", &m1, &n1);
printf("Enter rows and columns for Matrix B respectively: ");
scanf("%d%d", &m2, &n2);
if (n1 != m2)
{
printf("Matrix multiplication not possible.\n");
}
else
{
printf("Enter elements in Matrix A:\n");
for (i = 0; i < m1; i++)
for (j = 0; j < n1; j++)
{
scanf("%d", &a[i][j]);
}
printf("\nEnter elements in Matrix B:\n");
for (i = 0; i < m2; i++)
for (j = 0; j < n2; j++)
{
scanf("%d", &b[i][j]);
}
multiply(m1, n1, a, m2, n2, b, c);
}
printf("On matrix multiplication of A and B the result is:\n");
display(m1, n2, c);
}
void multiply (int m1, int n1, int a[10][10], int m2, int n2, int b[10][10], int c[10][10])
{
static int i = 0, j = 0, k = 0;
if (i >= m1)
{
return;
}
else if (i < m1)
{
if (j < n2)
{
if (k < n1)
{
c[i][j] += a[i][k] * b[k][j];
k++;
multiply(m1, n1, a, m2, n2, b, c);
}
k = 0;
j++;
multiply(m1, n1, a, m2, n2, b, c);
}
j = 0;
i++;
multiply(m1, n1, a, m2, n2, b, c);
}
}
void display(int m1, int n2, int c[10][10])
{
int i, j;
for (i = 0; i < m1; i++)
{
for (j = 0; j < n2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
Output
Enter rows and columns for Matrix A respectively: 2
2
Enter rows and columns for Matrix B respectively: 2
2
Enter elements in Matrix A:
12 56
45 78
Enter elements in Matrix B:
2 6
5 8
On matrix multiplication of A and B the result is:
304 520
480 894
Input Few Numbers & Perform Merge Sort on them using Recursion
#include
#include
void mergeSort(int [], int, int, int);
void partition(int [],int, int);
int main()
{
int list[50];
int i, size;
printf("Enter total number of elements:");
scanf("%d", &size);
printf("Enter the elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
partition(list, 0, size - 1);
printf("After merge sort:\n");
for(i = 0;i < size; i++)
{
printf("%d ",list[i]);
}
return 0;
}
void partition(int list[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
partition(list, low, mid);
partition(list, mid + 1, high);
mergeSort(list, low, mid, high);
}
}
void mergeSort(int list[],int low,int mid,int high)
{
int i, mi, k, lo, temp[50];
lo = low;
i = low;
mi = mid + 1;
while ((lo <= mid) && (mi <= high))
{
if (list[lo] <= list[mi])
{
temp[i] = list[lo];
lo++;
}
else
{
temp[i] = list[mi];
mi++;
}
i++;
}
if (lo > mid)
{
for (k = mi; k <= high; k++)
{
temp[i] = list[k];
i++;
}
}
else
{
for (k = lo; k <= mid; k++)
{
temp[i] = list[k];
i++;
}
}
for (k = low; k <= high; k++)
{
list[k] = temp[k];
}
}
Output
Enter total number of elements:5
Enter the elements:
12
36
22
76
54
After merge sort:
12 22 36 54 76
Find the Nth Fibonacci Number using Recursion
#include
#include
int fibo(int);
int main()
{
int num;
int result;
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &num);
if (num < 0)
{
printf("Fibonacci of negative number is not possible.\n");
}
else
{
result = fibo(num);
printf("The %d number in fibonacci series is %d\n", num, result);
}
return 0;
}
int fibo(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return(fibo(num - 1) + fibo(num - 2));
}
}
Output
Enter the nth number in fibonacci series: 8
The 8 number in fibonacci series is 21
$ a.out
Enter the nth number in fibonacci series: 12
The 12 number in fibonacci series is 144
Find the Biggest Number in an Array of Numbers using Recursion
#include
#include
int large(int[], int, int);
int main()
{
int size;
int largest;
int list[20];
int i;
printf("Enter size of the list:");
scanf("%d", &size);
printf("Printing the list:\n");
for (i = 0; i < size ; i++)
{
list[i] = rand() % size;
printf("%d\t", list[i]);
}
if (size == 0)
{
printf("Empty list\n");
}
else
{
largest = list[0];
largest = large(list, size - 1, largest);
printf("\nThe largest number in the list is: %d\n", largest);
}
}
int large(int list[], int size, int largest)
{
if (size == 1)
return largest;
if (size > -1)
{
if (list[size] > largest)
{
largest = list[size];
}
return(largest = large(list, size - 1, largest));
}
else
{
return largest;
}
}
Output
Enter size of the list:8
Printing the list:
7 6 1 3 1 7 2 4
The largest number in the list is: 7
Recursion to Search an Element in Array
#include
#include
int search(int [], int, int);
int main()
{
int size, index, key;
int list[20];
int count = 0;
int i;
clrscr();
printf("Enter the size of the list: ");
scanf("%d", &size);
index = size;
printf("Printing the list:\n");
for (i = 0; i < size; i++)
{
list[i] = rand() % size;
printf("%d\t", list[i]);
}
printf("\nEnter the key to search: ");
scanf("%d", &key);
while (index > 0)
{
index = search(list, index - 1, key);
printf("Key found at position: %d\n", index + 1);
count++;
}
if (!count)
printf("Key not found.\n");
return 0;
}
int search(int array[], int size, int key)
{
int location;
if (array[size] == key)
{
return size;
}
else if (size == -1)
{
return -1;
}
else
{
return (location = search(array, size - 1, key));
}
}
Output
Enter the size of the list: 10
Printing the list:
3 6 7 5 3 5 6 2 9 1
Enter the key to search: 5
Key found at position: 6
Key found at position: 4
#include
#include
#include
struct node
{
int a;
struct node *next;
};
void generate(struct node **);
void display(struct node *);
void stack_reverse(struct node **, struct node **);
void delete(struct node **);
int main()
{
struct node *head = NULL;
generate(&head);
printf("\nThe sequence of contents in stack\n");
display(head);
printf("\nInversing the contents of the stack\n");
if (head != NULL)
{
stack_reverse(&head, &(head->next));
}
printf("\nThe contents in stack after reversal\n");
display(head);
delete(&head);
return 0;
}
void stack_reverse(struct node **head, struct node **head_next)
{
struct node *temp;
if (*head_next != NULL)
{
temp = (*head_next)->next;
(*head_next)->next = (*head);
*head = *head_next;
*head_next = temp;
stack_reverse(head, head_next);
}
}
void display(struct node *head)
{
if (head != NULL)
{
printf("%d ", head->a);
display(head->next);
}
}
void generate(struct node **head)
{
int num, i;
struct node *temp;
printf("Enter length of list: ");
scanf("%d", &num);
for (i = num; i > 0; i--)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
if (*head == NULL)
{
*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}
void delete(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
Output
Enter length of list: 10
The sequence of contents in stack
1 2 3 4 5 6 7 8 9 10
Inversing the contents of the stack
The contents in stack after reversal
10 9 8 7 6 5 4 3 2 1
Implement Selection Sort Recursively
#include
#include
void selection(int [], int, int, int, int);
int main()
{
int list[30], size, temp, i, j;
printf("Enter the size of the list: ");
scanf("%d", &size);
printf("Enter the elements in list:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
selection(list, 0, 0, size, 1);
printf("The sorted list in ascending order is\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
return 0;
}
void selection(int list[], int i, int j, int size, int flag)
{
int temp;
if (i < size - 1)
{
if (flag)
{
j = i + 1;
}
if (j < size)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
selection(list, i, j + 1, size, 0);
}
selection(list, i + 1, 0, size, 1);
}
}
Output
Enter the size of the list: 5
Enter the elements in list:
23
45
64
12
34
The sorted list in ascending order is
12 23 34 45 64
Perform Matrix Multiplication using Recursion
#include
#include
void multiply(int, int, int [][10], int, int, int [][10], int [][10]);
void display(int, int, int[][10]);
int main()
{
int a[10][10], b[10][10], c[10][10] = {0};
int m1, n1, m2, n2, i, j, k;
printf("Enter rows and columns for Matrix A respectively: ");
scanf("%d%d", &m1, &n1);
printf("Enter rows and columns for Matrix B respectively: ");
scanf("%d%d", &m2, &n2);
if (n1 != m2)
{
printf("Matrix multiplication not possible.\n");
}
else
{
printf("Enter elements in Matrix A:\n");
for (i = 0; i < m1; i++)
for (j = 0; j < n1; j++)
{
scanf("%d", &a[i][j]);
}
printf("\nEnter elements in Matrix B:\n");
for (i = 0; i < m2; i++)
for (j = 0; j < n2; j++)
{
scanf("%d", &b[i][j]);
}
multiply(m1, n1, a, m2, n2, b, c);
}
printf("On matrix multiplication of A and B the result is:\n");
display(m1, n2, c);
}
void multiply (int m1, int n1, int a[10][10], int m2, int n2, int b[10][10], int c[10][10])
{
static int i = 0, j = 0, k = 0;
if (i >= m1)
{
return;
}
else if (i < m1)
{
if (j < n2)
{
if (k < n1)
{
c[i][j] += a[i][k] * b[k][j];
k++;
multiply(m1, n1, a, m2, n2, b, c);
}
k = 0;
j++;
multiply(m1, n1, a, m2, n2, b, c);
}
j = 0;
i++;
multiply(m1, n1, a, m2, n2, b, c);
}
}
void display(int m1, int n2, int c[10][10])
{
int i, j;
for (i = 0; i < m1; i++)
{
for (j = 0; j < n2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
Output
Enter rows and columns for Matrix A respectively: 2
2
Enter rows and columns for Matrix B respectively: 2
2
Enter elements in Matrix A:
12 56
45 78
Enter elements in Matrix B:
2 6
5 8
On matrix multiplication of A and B the result is:
304 520
480 894
Input Few Numbers & Perform Merge Sort on them using Recursion
#include
#include
void mergeSort(int [], int, int, int);
void partition(int [],int, int);
int main()
{
int list[50];
int i, size;
printf("Enter total number of elements:");
scanf("%d", &size);
printf("Enter the elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
partition(list, 0, size - 1);
printf("After merge sort:\n");
for(i = 0;i < size; i++)
{
printf("%d ",list[i]);
}
return 0;
}
void partition(int list[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
partition(list, low, mid);
partition(list, mid + 1, high);
mergeSort(list, low, mid, high);
}
}
void mergeSort(int list[],int low,int mid,int high)
{
int i, mi, k, lo, temp[50];
lo = low;
i = low;
mi = mid + 1;
while ((lo <= mid) && (mi <= high))
{
if (list[lo] <= list[mi])
{
temp[i] = list[lo];
lo++;
}
else
{
temp[i] = list[mi];
mi++;
}
i++;
}
if (lo > mid)
{
for (k = mi; k <= high; k++)
{
temp[i] = list[k];
i++;
}
}
else
{
for (k = lo; k <= mid; k++)
{
temp[i] = list[k];
i++;
}
}
for (k = low; k <= high; k++)
{
list[k] = temp[k];
}
}
Output
Enter total number of elements:5
Enter the elements:
12
36
22
76
54
After merge sort:
12 22 36 54 76
Find the Nth Fibonacci Number using Recursion
#include
#include
int fibo(int);
int main()
{
int num;
int result;
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &num);
if (num < 0)
{
printf("Fibonacci of negative number is not possible.\n");
}
else
{
result = fibo(num);
printf("The %d number in fibonacci series is %d\n", num, result);
}
return 0;
}
int fibo(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return(fibo(num - 1) + fibo(num - 2));
}
}
Output
Enter the nth number in fibonacci series: 8
The 8 number in fibonacci series is 21
$ a.out
Enter the nth number in fibonacci series: 12
The 12 number in fibonacci series is 144
Find the Biggest Number in an Array of Numbers using Recursion
#include
#include
int large(int[], int, int);
int main()
{
int size;
int largest;
int list[20];
int i;
printf("Enter size of the list:");
scanf("%d", &size);
printf("Printing the list:\n");
for (i = 0; i < size ; i++)
{
list[i] = rand() % size;
printf("%d\t", list[i]);
}
if (size == 0)
{
printf("Empty list\n");
}
else
{
largest = list[0];
largest = large(list, size - 1, largest);
printf("\nThe largest number in the list is: %d\n", largest);
}
}
int large(int list[], int size, int largest)
{
if (size == 1)
return largest;
if (size > -1)
{
if (list[size] > largest)
{
largest = list[size];
}
return(largest = large(list, size - 1, largest));
}
else
{
return largest;
}
}
Output
Enter size of the list:8
Printing the list:
7 6 1 3 1 7 2 4
The largest number in the list is: 7
Recursion to Search an Element in Array
#include
#include
int search(int [], int, int);
int main()
{
int size, index, key;
int list[20];
int count = 0;
int i;
clrscr();
printf("Enter the size of the list: ");
scanf("%d", &size);
index = size;
printf("Printing the list:\n");
for (i = 0; i < size; i++)
{
list[i] = rand() % size;
printf("%d\t", list[i]);
}
printf("\nEnter the key to search: ");
scanf("%d", &key);
while (index > 0)
{
index = search(list, index - 1, key);
printf("Key found at position: %d\n", index + 1);
count++;
}
if (!count)
printf("Key not found.\n");
return 0;
}
int search(int array[], int size, int key)
{
int location;
if (array[size] == key)
{
return size;
}
else if (size == -1)
{
return -1;
}
else
{
return (location = search(array, size - 1, key));
}
}
Output
Enter the size of the list: 10
Printing the list:
3 6 7 5 3 5 6 2 9 1
Enter the key to search: 5
Key found at position: 6
Key found at position: 4
No comments:
Post a Comment