Wednesday, October 29, 2014

C Struct node Pro



Find Number of Occurrences of All Elements in a Linked List

#include 
#include 
#include 
struct node
{
    int num;
    struct node *next;
};
struct node_occur
{
    int num;
    int times;
    struct node_occur *next;
};
void create(struct node **);
void occur(struct node *, struct node_occur **);
void release(struct node **);
void release_2(struct node_occur **);
void display(struct node *);
void disp_occur(struct node_occur *);
int main()
{
    struct node *p = NULL;
    struct node_occur *head = NULL;
    int n;
    printf("Enter data into the list\n");
    create(&p);
    printf("Displaying the occurence of each node in the list:\n");
    display(p);
    occur(p, &head);
    disp_occur(head);
    release(&p);
    release_2(&head);
     return 0;
}
 void occur(struct node *head, struct node_occur **result)
{
    struct node *p;
    struct node_occur *temp, *prev;
    p = head;
    while (p != NULL)
    {
        temp = *result;
        while (temp != NULL && temp->num != p->num)
        {
            prev = temp;
            temp = temp->next;
        }
        if (temp == NULL)
        {
            temp = (struct node_occur *)malloc(sizeof(struct node_occur));
            temp->num = p->num;
            temp->times = 1;
            temp->next = NULL;
            if (*result != NULL)
            {
                prev->next = temp;
            }
            else
            {
                *result = temp;
            }
        }
        else
        {
            temp->times += 1;
        }
        p = p->next;
    }
}
void create(struct node **head)
{
    int c, ch;
    struct node *temp, *rear;
    do
    {
        printf("Enter number: ");
        scanf("%d", &c);
        temp = (struct node *)malloc(sizeof(struct node));
        temp->num = c;
        temp->next = NULL;
        if (*head == NULL)
        {
            *head = temp;
        }
        else
        {
            rear->next = temp;
        }
        rear = temp;
        printf("Do you wish to continue [1/0]: ");
        scanf("%d", &ch);
    } while (ch != 0);
    printf("\n");
}
void display(struct node *p)
{
    while (p != NULL)
    {
        printf("%d\t", p->num);
        p = p->next;
    }
    printf("\n");
}
 void disp_occur(struct node_occur *p)
{
    printf("***************************\n  Number\tOccurence\n***************************\n");
    while (p != NULL)
    {
        printf("    %d\t\t%d\n", p->num, p->times);
        p = p->next;
    }
}
 void release(struct node **head)
{
    struct node *temp = *head;
    *head = (*head)->next;
    while ((*head) != NULL)
    {
        free(temp);
        temp = *head;
        (*head) = (*head)->next;
    }
}
void release_2(struct node_occur **head)
{
    struct node_occur *temp = *head;
    *head = (*head)->next;
    while ((*head) != NULL)
    {
        free(temp);
        temp = *head;
        (*head) = (*head)->next;
    }
}

Output
            Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 6
Do you wish to continue [1/0]: 1
Enter number: 1
Do you wish to continue [1/0]: 0
Displaying the occurence of each node in the list:
1          2          3          2          4          2          6          1          
***************************
  Number         Occurence
***************************
    1                  2
    2                  3
    3                  1
    4                  1
    6                  1

 

Find the first Common Element between the 2 given Linked Lists

#include 
#include 
#include 
struct node
{
    int num;
    struct node *next;
};
void create(struct node **);
int find(struct node *, struct node *);
void release(struct node **);
void display(struct node *);
int main()
{
    struct node *p = NULL, *q = NULL;
    int result;
    printf("Enter data into the list\n");
    create(&p);
    printf("Enter data into the list\n");
    create(&q);
    printf("Displaying list1:\n");
    display(p);
    printf("Displaying list2:\n");
    display(q);
    result = find(p, q);
    if (result)
    {
        printf("The first matched element is %d.\n", result);
    }
    else
    {
        printf("No matching element found.\n");
    }
    release (&p);
    return 0;
}
int find(struct node *p, struct node *q)
{
    struct node *temp;
    while (p != NULL)
    {
        temp = q;
        while (temp != NULL)
        {
            if (temp->num == p->num)
            {
                return p->num;
            }
            temp = temp->next;
        }
        p = p->next;
    }
     return 0;
}
void create(struct node **head)
{
    int c, ch;
    struct node *temp, *rear;
    do
    {
        printf("Enter number: ");
        scanf("%d", &c);
        temp = (struct node *)malloc(sizeof(struct node));
        temp->num = c;
        temp->next = NULL;
        if (*head == NULL)
        {
            *head = temp;
        }
        else
        {
            rear->next = temp;
        }
        rear = temp;
        printf("Do you wish to continue [1/0]: ");
        scanf("%d", &ch);
    } while (ch != 0);
    printf("\n");
}
void display(struct node *head)
{
    while (head != NULL)
    {
        printf("%d\t", head->num);
        head = head->next;
    }
    printf("\n");
}
void release(struct node **head)
{
    struct node *temp;
    while ((*head) != NULL)
    {
        temp = *head;
        (*head) = (*head)->next;
        free(temp);
    }
}

Output
 
Enter data into the list1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 8
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 1
Enter number: 6
Do you wish to continue [1/0]: 0
Enter data into the list2
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 1
Enter number: 9
Do you wish to continue [1/0]: 0
Displaying list1:
2          8          5          6          
Displaying list2:
3          5          9          
The first matched element is 5.

No comments:

Post a Comment