Monday, October 27, 2014

NEW C THEORY

Search for an Element in the Linked List using Recursion

#include 
#include 
#include 
 
struct node
{
    int a;
    struct node *next;
};
 
void generate(struct node **, int);
void search(struct node *, int, int);
void delete(struct node **);
 
int main()
{
    struct node *head;
    int key, num;
    clrscr();
    printf("Enter the number of nodes: ");
    scanf("%d", &num);
    generate(&head, num);
    printf("\nEnter key to search: ");
    scanf("%d", &key);
    search(head, key, num);
    delete(&head);
}
 
void generate(struct node **head, int num)
{
    int i;
    struct node *temp;
 
    for (i = 0; i < num; i++)
    {
        temp = (struct node *)malloc(sizeof(struct node));
        temp->a = rand() % num;
        printf("%d    ", temp->a);
        if (*head == NULL)
        {
            *head = temp;
            (*head)->next = NULL;
        }
        else
        {
            temp->next = *head;
            *head = temp;
        }
    }
}
 
void search(struct node *head, int key, int index)
{
    if (head->a == key)
    {
        printf("Key found at Position: %d\n", index);
    }
    if (head->next == NULL)
    {
        return;
    }
    search(head->next, key, index - 1);
}
 
void delete(struct node **head)
{
    struct node *temp;
    while (*head != NULL)
    {
        temp = *head;
        *head = (*head)->next;
        free(temp);
    }
}

Output
 
Enter the number of nodes: 6
1    4    3    1    5    1
Enter key to search: 1
Key found at Position: 6
Key found at Position: 4
Key found at Position: 1

Reverse the String using Recursion

#include 
#include 
#include 
 
void reverse(char [], int, int);
int main()
{
    char str1[20];
    int size;
 
    printf("Enter a string to reverse: ");
    scanf("%s", str1);
    size = strlen(str1);
    reverse(str1, 0, size - 1);
    printf("The string after reversing is: %s\n", str1);
    return 0;
}
 
void reverse(char str1[], int index, int size)
{
    char temp;
    temp = str1[index];
    str1[index] = str1[size - index];
    str1[size - index] = temp;
    if (index == size / 2)
    {
        return;
    }
    reverse(str1, index + 1, size);
}

Output
 
Enter a string to reverse: malayalam
The string after reversing is: malayalam
 
Enter a string to reverse: cprogramming
The string after reversing is: gnimmargorpc

 

Count the Number of Occurrences of an Element in the Linked List using Recursion

#include 
#include  
void occur(int [], int, int, int, int *);
 
int main()
{
    int size, key, count = 0;
    int list[20];
    int i;
 
    printf("Enter the size of the list: ");
    scanf("%d", &size);
    printf("Printing the list:\n");
    for (i = 0; i < size; i++)
    {
        list[i] = rand() % size;
        printf("%d    ", list[i]);
    }
    printf("\nEnter the key to find it's occurence: ");
    scanf("%d", &key);
    occur(list, size, 0, key, &count);
    printf("%d occurs for %d times.\n", key, count);
    return 0;
}
 
void occur(int list[], int size, int index, int key, int *count)
{
    if (size == index)
    {
        return;
    }
    if (list[index] == key)
    {
        *count += 1;
    }
    occur(list, size, index + 1, key, count);
}

Output
 
Enter the size of the list: 7
Printing the list:
1    4    2    5    1    3    3
Enter the key to find it's occurence: 3
3 occurs for 2 times.

Count the Number of Occurrences of an Element in the Linked List using Recursion

#include 
 
void occur(int [], int, int, int, int *);
 
int main()
{
    int size, key, count = 0;
    int list[20];
    int i;
 
    printf("Enter the size of the list: ");
    scanf("%d", &size);
    printf("Printing the list:\n");
    for (i = 0; i < size; i++)
    {
        list[i] = rand() % size;
        printf("%d    ", list[i]);
    }
    printf("\nEnter the key to find it's occurence: ");
    scanf("%d", &key);
    occur(list, size, 0, key, &count);
    printf("%d occurs for %d times.\n", key, count);
    return 0;
}
 
void occur(int list[], int size, int index, int key, int *count)
{
    if (size == index)
    {
        return;
    }
    if (list[index] == key)
    {
        *count += 1;
    }
    occur(list, size, index + 1, key, count);
}

Output
 
Enter the size of the list: 7
Printing the list:
1    4    2    5    1    3    3
Enter the key to find it's occurence: 3
3 occurs for 2 times.

 

 

 

Display all the Nodes in a Linked List using Recursion

#include 
#include 
#include 
 
struct node
{
    int a;
    struct node *next;
};
 
void generate(struct node **);
void display(struct node*);
void delete(struct node **);
 
int main()
{
    struct node *head = NULL;
 
    generate(&head);
    display(head);
    delete(&head);
    return 0;
}
 
void generate(struct node **head)
{
    int num = 10, i;
    struct node *temp;
 
    for (i = 0; i < num; 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 display(struct node *head)
{
    printf("%d    ", head->a);
    if (head->next == NULL)
    {
        return;
    }
    display(head->next);
}
 
void delete(struct node **head)
{
    struct node *temp;
    while (*head != NULL)
    {
        temp = *head;
        *head = (*head)->next;
        free(temp);
    }
}

Output
 
 
9    8    7    6    5    4    3    2    1    0

Find Sum of Digits of a Number using Recursion

#include 
#include 
int sum (int a);
 
int main()
{
int num, result;
 
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}
 
int sum (int num)
{
if (num != 0)
{
return (num % 10 + sum (num / 10));
}
else
{
return 0;
}
}

Output
 
Enter the number: 2345
Sum of digits in 2345 is 14

Find the Length of the Linked List using Recursion

#include 
#include 
int length(char [], int);
int main()
{
    char word[20];
    int count;
 
    printf("Enter a word to count it's length: ");
    scanf("%s", word);
    count = length(word, 0);
    printf("The number of characters in %s are %d.\n", word, count);
    return 0;
}
 
int length(char word[], int index)
{
    if (word[index] == '\0')
    {
        return 0;
    }
    return (1 + length(word, index + 1));
}

 
 
 
Output
 
Enter a word to count it's length: 5
The number of characters in 5 are 1.
 
Enter a word to count it's length: sanfoundry
The number of characters in sanfoundry are 10.

Find Reverse of a Number using Recursion

#include 
#include 
#include 
 
int rev(int, int);
 
int main()
{
    int num, result;
    int length = 0, temp;
    clrscr();
    printf("Enter an integer number to reverse: ");
    scanf("%d", &num);
    temp = num;
    while (temp != 0)
    {
        length++;
        temp = temp / 10;
    }
    result = rev(num, length);
    printf("The reverse of %d is %d.\n", num, result);
    return 0;
}
 
int rev(int num, int len)
{
    if (len == 1)
    {
        return num;
    }
    else
    {
        return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len));
    }
}

Output
 
Enter an integer number to reverse: 1234
The reverse of 1234 is 4321.

Find Sum of N Numbers using Recursion

#include 
#include 
void display(int);
 
int main()
{
    int num, result;
 
    printf("Enter the Nth number: ");
    scanf("%d", &num);
    display(num);
    return 0;
}
 
void display(int num)
{
    static int i = 1;
 
    if (num == i)
    {
        printf("%d   \n", num);
        return;
    }
    else
    {
        printf("%d   ", i);
        i++;
        display(num);
    }
}

Output
 
Enter the Nth number: 10
1   2   3   4   5   6   7   8   9   10

Find HCF of a given Number using Recursion

#include 
#include 
int hcf(int, int);
 
int main()
{
    int a, b, result;
    clrscr();
    printf("Enter the two numbers to find their HCF: ");
    scanf("%d%d", &a, &b);
    result = hcf(a, b);
    printf("The HCF of %d and %d is %d.\n", a, b, result);
}
 
int hcf(int a, int b)
{
    while (a != b)
    {
        if (a > b)
        {
            return hcf(a - b, b);
        }
        else
        {
            return hcf(a, b - a);
        }
    }
    return a;
}

Output
 
Enter the two numbers to find their HCF: 24 36
The HCF of 24 and 36 is 12.





No comments:

Post a Comment