Challenge

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

#include
#include

struct node //structure is initialized

{
    int data;
    struct node *next;

} *list1, *list2, *unionlist, *intersectionlist;

void initialize() //`initializing the both list as NULL

{
    list1 = NULL;
    list2 = NULL;
}

void insert(struct node **head, int numb)

{
    struct node* newNode = (struct node*) malloc(sizeof(struct node)); //creating link list node
    newNode->data = numb;
    newNode->next = *head;
    *head = newNode;
}

int search(struct node *head, int numb) // Searches an element in Linked List by linearly traversing from head to tail


{
    while (head != NULL)

      {
          if (head->data == numb)

            {
             return 1;
             }
          head = head->next;
       }
   return 0;

}

struct node* findunion(struct node *list1, struct node *list2)

{
    unionlist = NULL;
    struct node *temp = list1;
    while(temp != NULL)

{
    insert(&unionlist, temp->data);
    temp = temp->next;
}

while(list2 != NULL)

{
    if(!search(list1, list2->data))

     {
        insert(&unionlist, list2->data); //inserting the nodes of list2 to list1
      }
    list2 = list2->next;
}
return unionlist; //return the union
}


struct node* intersection(struct node *list1, struct node *list2)

{
    intersectionlist = NULL;

    while(list1 != NULL)

      {
          if(search(list2, list1->data)) //comparing the 2 list and if elements are present in both list it is added to the intersection list

          {
           insert(&intersectionlist, list1->data);
           }
          list1 = list1->next;
       }
     return intersectionlist;
}

void printLinkedList(struct node *nodePtr)

{
    while (nodePtr != NULL)

      {
         printf("%d ", nodePtr->data);
         nodePtr = nodePtr->next;
      }
}

int main()

{
     int i, listno1, listno2, temp;
     initialize();
     printf("Enter number of elements in first Linked List:");
     fflush(stdout);
     scanf("%d", &listno1); //input the number of elements from the user
     printf("Enter %d elements\n", listno1);
     for(i=0; i

       {
          fflush(stdout);
          scanf("%d", &temp);
          insert(&list1, temp);
        }
    printLinkedList(list1); //printing the link list 1
    printf("\nEnter number of elements in second Linked List:");
    fflush(stdout);
    scanf("%d", &listno2); //input the number of elements from the user
    printf("Enter %d elements\n", listno2);
    for(i=0; i

       {
          fflush(stdout);
          scanf("%d", &temp);
          insert(&list2, temp);
        }
    printLinkedList(list2);//printing the link list 2

    findunion(list1, list2); //finding the union members of 2 list
    intersection(list1, list2); //finding the intersection of 2 list
    printf("\nUnion:");
    printLinkedList(unionlist); //printing the union
    printf("\nIntersection:");
    printLinkedList(intersectionlist); //printing the intersection

    return 0;
}

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.