Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)

Project-1:

Code:

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *nextptr;
}*temp,*rear, *intrsct, *uni,*set1, *set2;

void CreateSet(struct node **set1);
void UnionCreation(struct node *set1, struct node *set2, struct node **uni);
void IntersectionCreation(struct node *set1, struct node *set2, struct node **intrsct);
void DisplaySet(struct node *set1);


int main()
{
	//variable declaration
    int i,n1,n2;

    //structure initialisation
    set1 = set2 = intrsct = uni = NULL;

    //Entering elements in Set-1
    printf("Enter databer of elements in Set-1: ");
    scanf("%d", &n1);
    for (i=0;i<n1;i++)
    {
    CreateSet(&set1);
    }

    //Entering elements in Set-2
    printf("Enter databer of elements in Set-2: ");
    scanf("%d", &n2);
    for (i=0;i<n2;i++)
    {
    CreateSet(&set2);
    }

    UnionCreation(set1, set2, &uni);//Creation of Union Set
    IntersectionCreation(set1, set2, &intrsct);//Creation of Intersection Set

    //Printing data in Set-1
    printf("\nData elements in Set-1:\n");
    if (set1 == NULL)
    	printf("Null\n");
    else
    	DisplaySet(set1);

    //Printing data in Set-2
    printf("Data elements in Set-2:\n");
    if (set2 == NULL)
    	printf("Null\n");
    else
    	DisplaySet(set2);

    //Printing data in Union
    printf("Data elements in Union:\n");
    if (uni == NULL)
    	printf("Null\n");
    else
    	DisplaySet(uni);

    //Printing data in Intersection
    printf("Data elements in Intersection:\n");
    if (intrsct == NULL)
    	printf("Null\n");
    else
    	DisplaySet(intrsct);

    return 0;
}

void IntersectionCreation(struct node *set1, struct node *set2, struct node **intrsct)
{
    struct node *temp_1, *temp_2, *temp_itr, *newNode_itr, *head_itr;

    temp_1 = set1;
    while (temp_1 != NULL)
    {
        temp_2 = set2;
        head_itr = *intrsct;
        while (temp_2 != NULL && temp_1->data != temp_2->data)
        {
            temp_2 = temp_2->nextptr;
        }
        if (temp_2 != NULL)
        {
            if (head_itr != NULL)
            {
                if (head_itr->data == temp_2->data)
                {
                    temp_1 = temp_1->nextptr;
                    continue;
                }
                head_itr = head_itr->nextptr;
            }
            temp_itr = (struct node *)malloc(sizeof(struct node));
            temp_itr->data = temp_2->data;
            temp_itr->nextptr = NULL;
            if (*intrsct == NULL)
            {
                *intrsct = temp_itr;
            }
            else
            {
                newNode_itr->nextptr = temp_itr;
            }
            newNode_itr = temp_itr;
        }
        temp_1 = temp_1->nextptr;
    }
}

void UnionCreation(struct node *set1, struct node *set2, struct node **uni)
{
    struct node *temp_uni, *head_uni, *newNode_uni;
    int flg_uni = 0;

    while (set1 != NULL)
    {
        head_uni = *uni;
        while (head_uni != NULL)
        {
            if (head_uni->data == set1->data)
            {
                flg_uni = 1;
            }
            head_uni = head_uni->nextptr;
        }
        if (flg_uni)
        {
            flg_uni = 0;
        }
        else
        {
            temp_uni = (struct node *)malloc(sizeof(struct node));
            temp_uni->data = set1->data;
            temp_uni->nextptr = NULL;
            if (*uni == NULL)
            {
                *uni = temp_uni;
            }
            else
            {
                newNode_uni->nextptr = temp_uni;
            }
            newNode_uni = temp_uni;
        }
        set1 = set1->nextptr;
    }
    while (set2!= NULL)
    {
        head_uni = *uni;
        while (head_uni != NULL)
        {
            if (head_uni->data == set2->data)
            {
                flg_uni = 1;
            }
            head_uni = head_uni->nextptr;
        }
        if (flg_uni)
        {
            flg_uni = 0;
        }
        else
        {
            temp_uni = (struct node *)malloc(sizeof(struct node));
            temp_uni->data = set2->data;
            temp_uni->nextptr = NULL;
            if (*uni == NULL)
            {
                *uni = temp_uni;
            }
            else
            {
                newNode_uni->nextptr = temp_uni;
            }
            newNode_uni = temp_uni;
        }
        set2 = set2->nextptr;
    }
}

void CreateSet(struct node **head)
{
    //struct node *temp, *rear;
    int a;


        printf("Enter element: ");
        scanf("%d", &a);
        temp = (struct node *)malloc(sizeof(struct node));
        temp->data = a;
        temp->nextptr = NULL;
        if (*head == NULL)
        {
            *head = temp;
        }
        else
        {
            rear->nextptr = temp;
        }
        rear = temp;

}

void DisplaySet(struct node *head)
{
    while (head != NULL)
    {
        printf("%d   ", head->data);
        head = head->nextptr;
    }
    printf("\n");
}


Output:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.