Challenge

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

NAME: Madhu D

BATCH: LIVE BATCH 12

PROGRAM:

#include <stdio.h>  //Adding standard input output header file
#include <stdlib.h> //adding standard library header file
// A structure of linked list node
struct node
{
     int data;
     struct node *next;

} *List1, *List2, *Unionlist, *intersectionlist;
void initialize()
{
      List1 = List2 = NULL;
}
//Inserting a node in front of a singly linked list.
void insert(struct node **head, int v)
{
     // Create a new Linked List node
     struct node* newNode = (struct node*) malloc(sizeof(struct node));
     newNode->data = v;
     // Pointer of new node will point to head node of linked list
     newNode->next = *head;
     //make new node as new head of linked list
     *head = newNode;
}
// Searches an element in Linked List by linearly traversing from head to tail
int search(struct node *head, int v)
{
     while (head != NULL)
   {
      if (head->data == v)
      {
           return 1;
       }
      head = head->next;
    }
  return 0;
}
//Returns the union of two given linked list
struct node* findunion(struct node *List1, struct node *List2)
{
    Unionlist = NULL;
    // Add all nodes of first Linked List to Unionlist
    struct node *temp = List1;
    while(temp != NULL)
   {
        insert(&Unionlist, temp->data);
        temp = temp->next;
    }
// Insert those nodes of List2 which is not present in List1
   while(List2 != NULL)
  {
      if(!search(List1, List2->data))
        {
             insert(&Unionlist, List2->data);
         }
         List2 = List2->next;
   }
  return Unionlist;
}

//Returns the Linked List which contains common nodes of two given linked list
struct node* intersection(struct node *List1, struct node *List2)
{
     intersectionlist = NULL;
     //Search every element of List1 in List2, If found then add it to intersection List
     while(List1 != NULL)
   {
         if(search(List1, List2->data))
        {
            insert(&intersectionlist, List1->data);
         }
         List1 = List1->next;
    }
     return intersectionlist;
}
//Prints a linked list from head node till tail node
void printLinkedList(struct node *nodePtr)
{
    while (nodePtr != NULL)
     {
        printf("%d", nodePtr->data);
        nodePtr = nodePtr->next;
        if(nodePtr != NULL)
         printf("-->");
      }
 }
int main()
{
   int i, List1Count, List2Count, temp;
    initialize();
    //Creating the First List
    printf("Enter the number of nodes in 1st List\n");
    scanf("%d", &List1Count);
    printf("Enter %d integers of List1\n", List1Count);
    for(i=0; i<List1Count; i++)
    {
        scanf("%d", &temp);
        insert(&List1Count, temp);
     }
      printLinkedList(List1);
      printf("Enter number of nodes in 2nd List\n");
      scanf("%d", &List2Count);
      printf("Enter %d integers of List2\n", List2Count);
      for(i=0; i<List2Count; i++)
      {
         scanf("%d", &temp);
         insert(&List2, temp);
       }
      printLinkedList(List2);
      //printing the Union of two given linked list
      findunion(List1, List2);
      intersection(List1, List2);
      printf("Union Linked List\n\n");
      printLinkedList(Unionlist);
      printf("Intersection Linked List\n");
      printLinkedList(intersectionlist);
   return 0;
}

PROGRAM:

#include <stdio.h> //Adding standard input output header file
#include <stdlib.h> //adding standard library header file
// A structure of linked list node
struct node
{
int data;
struct node *next;

} *List1, *List2, *Unionlist, *intersectionlist;
void initialize()
{
List1 = List2 = NULL;
}
//Inserting a node in front of a singly linked list.
void insert(struct node **head, int v)
{
// Create a new Linked List node
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = v;
// Pointer of new node will point to head node of linked list
newNode->next = *head;
//make new node as new head of linked list
*head = newNode;
}
// Searches an element in Linked List by linearly traversing from head to tail
int search(struct node *head, int v)
{
while (head != NULL)
{
if (head->data == v)
{
return 1;
}
head = head->next;
}
return 0;
}
//Returns the union of two given linked list
struct node* findunion(struct node *List1, struct node *List2)
{
Unionlist = NULL;
// Add all nodes of first Linked List to Unionlist
struct node *temp = List1;
while(temp != NULL)
{
insert(&Unionlist, temp->data);
temp = temp->next;
}
// Insert those nodes of List2 which is not present in List1
while(List2 != NULL)
{
if(!search(List1, List2->data))
{
insert(&Unionlist, List2->data);
}
List2 = List2->next;
}
return Unionlist;
}

//Returns the Linked List which contains common nodes of two given linked list
struct node* intersection(struct node *List1, struct node *List2)
{
intersectionlist = NULL;
//Search every element of List1 in List2, If found then add it to intersection List
while(List1 != NULL)
{
if(search(List1, List2->data))
{
insert(&intersectionlist, List1->data);
}
List1 = List1->next;
}
return intersectionlist;
}
//Prints a linked list from head node till tail node
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{
int i, List1Count, List2Count, temp;
initialize();
//Creating the First List
printf("Enter the number of nodes in 1st List\n");
scanf("%d", &List1Count);
printf("Enter %d integers of List1\n", List1Count);
for(i=0; i<List1Count; i++)
{
scanf("%d", &temp);
insert(&List1Count, temp);
}
printLinkedList(List1);
printf("Enter number of nodes in 2nd List\n");
scanf("%d", &List2Count);
printf("Enter %d integers of List2\n", List2Count);
for(i=0; i<List2Count; i++)
{
scanf("%d", &temp);
insert(&List2, temp);
}
printLinkedList(List2);
//printing the Union of two given linked list
findunion(List1, List2);
intersection(List1, List2);
printf("Union Linked List\n\n");
printLinkedList(Unionlist);
printf("Intersection Linked List\n");
printLinkedList(intersectionlist);
return 0;
}

RESULT:

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.