Monday, January 5, 2015

Linked list using recursion and String using Recursion in C program



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 
#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.

No comments:

Post a Comment