Wednesday, October 29, 2014

C Nodes Pro



Display the Nodes of a Linked List in Reverse without using Recursion

#include 
#include 
#include 
struct node
{
    int visited;
    int a;
    struct node *next;
};
void generate(struct node **);
void display(struct node *);
void linear(struct node *);
void delete(struct node **);
 
int main()
{
    struct node *head = NULL;
    generate(&head);
    printf("\nPrinting the list in linear order\n");
    linear(head);
    printf("\nPrinting the list in reverse order\n");
    display(head);
    delete(&head);
    return 0;
}
void display(struct node *head)
{
    struct node *temp = head, *prev = head;
    while (temp->visited == 0)
    {
        while (temp->next != NULL && temp->next->visited == 0)
        {
            temp = temp->next;
        }
        printf("%d  ", temp->a);
        temp->visited = 1;
        temp = head;
    }    
}
void linear(struct node *head)
{
    while (head != NULL)
    {
        printf("%d  ", head->a);
        head = head->next;
    }
    printf("\n");
}
 
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;
        temp->visited = 0;
        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: 5
Printing the list in linear order
1  2  3  4  5  
Printing the list in reverse order
5  4  3  2  1

Search for an Element in the Linked List without using Recursion

#include 
#include 
#include 
struct node
{
    int a;
    struct node *next;
};
void generate(struct node **, int);
void search(struct node *, int);
void delete(struct node **);
 int main()
{
    struct node *head = NULL;
    int key, num;
    clrscr();
    printf("Enter the number of nodes: ");
    scanf("%d", &num);
    printf("\nDisplaying the list\n");
    generate(&head, num);
    printf("\nEnter key to search: ");
    scanf("%d", &key);
    search(head, key);
    delete(&head);
 
    return 0;
}
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;
        if (*head == NULL)
        {
            *head = temp;
            temp->next = NULL;
        }
        else
        {
            temp->next = *head;
            *head = temp;
        }
        printf("%d  ", temp->a);
    }
}
void search(struct node *head, int key)
{
    while (head != NULL)
    {
        if (head->a == key)
        {
            printf("key found\n");
            return;
        }
        head = head->next;
    }
    printf("Key not found\n");
}
void delete(struct node **head)
{
    struct node *temp;
    while (*head != NULL)
    {
        temp = *head;
        *head = (*head)->next;
        free(temp);
    }
}

Output
 
Enter the number of nodes: 10
Displaying the list
3  6  7  5  3  5  6  2  9  1  
Enter key to search: 2
key found

Find the First Capital Letter in a String without using Recursion

#include 
#include 
#include 
#include 
char caps_check(char *);
int main()
{
    char string[20], letter;
    clrscr();
    printf("Enter a string to find it's first capital letter: ");
    scanf("%s", string);
    letter = caps_check(string);
    if (letter == 0)
    {
        printf("No capital letter is present in %s.\n", string);
    }
    else
    {
        printf("The first capital letter in %s is %c.\n", string, letter);    }
        return 0;
    }
    char caps_check(char *string)
    {
        int i = 0;
        while (string[i] != '\0')
        {
            if (isupper(string[i]))
            {
                return string[i];
            }
            i++;
        }
        return 0;
    }
Output
 
Enter a string to find it's first capital letter: prOgraMmInG
The first capital letter in prOgraMmInG is O.

No comments:

Post a Comment