Monday, January 5, 2015

C using Linked List and using recursion and Number using Recursion in pro



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.

No comments:

Post a Comment