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
 
//self referntial structure for creating linked list 
struct Record
{
int data;                               //variable for data storage
struct Record *next;                   //pointer to hold the structure 
};
void add_begin(struct Record **head);
void add(struct Record **head,int d);
void print(struct Record *print);
struct Record *intersection_fun(struct Record *List1,struct Record *List2);
struct Record *union_fun(struct Record *List1,struct Record *List2);
 
//this function is used for creating linked list 
void add_begin(struct Record **head)
{
 
struct Record *new =malloc(sizeof(struct Record));     //allocatiing memory to newly created node 
   printf("enter the data \n");                            
   scanf("%d",&new->data);                                 //reading the data
   new->next=*head;                                        //updating  next address ofthe newly created  node 
   *head=new;                                           // updating the head pointer
}
 
//this function is used in the  intersection and union 
void add(struct Record **head,int d)
{
 
struct Record *new =malloc(sizeof(struct Record));    //allocatiing memory to newly created node         
     new->data=d;                                         //assign value to newly creadted node
   new->next=*head;                                    //updating  next address ofthe newly created  node 
   *head=new;                                        // updating the head pointer
}
//this function is used for displaying the lists
void print(struct Record *print)
{
while(print!=NULL)                          //loop rotates untill NULL reached 
{
 
printf("%d ",print->data);       //displays the data 
print=print->next;              //move to the next location
   } 
}
struct Record *intersection_fun(struct Record *List1,struct Record *List2)
{
struct Record *Result=NULL,*L2,*R;
int flag=1;                      
while(List1!=NULL)              //rotates loop untill NUll reached
{   L2=List2;                   //every update the L2 with List2 address
while(L2!=NULL)             //rotates loop untill NULL reached
{
if(List1->data==L2->data)  //comparing list1 data is present in list2 data
{    
     R=Result;           
while(R!=NULL)          //rotates loop untill null reached 
{
    if(List1->data==R->data)     //for avoiding duplicates in intersection result 
       {
        flag=0;
   }
else
{
flag=1;
}
R=R->next;
} 
if(flag)        //if not present in R then only added to Result list 
{
 
  add(&Result,List1->data);
    }
}
L2=L2->next;              //move to the next address
}
List1=List1->next;          //move to the next address
}
 
return Result;
}
struct Record *union_fun(struct Record *List1,struct Record *List2)
{
struct Record *Result=NULL,*ptr1,*ptr2,*R;
int send=1;
 
    ptr1=List1;
    ptr2=List2;
while(ptr1!=NULL)         //rotates loop untill null reached 
{
add(&Result,ptr1->data); //add to result list 
ptr1=ptr1->next;          //move to the next location
}
 
while(ptr2!=NULL)              //rotates loop untill null reached 
{
ptr1=Result;
while(ptr1!=NULL)            //rotates loop untill null reached
{
if(ptr2->data==ptr1->data)  //for avoiding to duplicates in result
{
send=0;
break;
}
else
{
send=1;
}
ptr1=ptr1->next;
}
if(send)
{
add(&Result,ptr2->data);  //add only when it is present in result
}
 
 
ptr2=ptr2->next;
}
 
   
return Result;
}
void main()
{
struct Record *List1=NULL;             //creating structure pointer 
struct Record *List2=NULL;             //creating structure pointer 
struct Record *Intersection_List=NULL; //creating structure pointer 
struct Record *Union_List=NULL;        //creating structure pointer 
  int n1,n2,i;
  printf("enter the number of elements in list1\n");  // asking how many elements did the user want in list 1
  scanf("%d",&n1);                     //reading  user input 
  printf("\n"); 
  for(i=0;i
  {
  add_begin(&List1);               // adding to the linked list1 
   
  }
  
  printf("enter the number of elements in  list2\n"); //asking how many elements did the user want in list 2
  scanf("%d",&n2);               //reading  user input 
  for(i=0;i
  {
  add_begin(&List2);          // adding to the linked list2
  }
   printf("\\nList1=");
   print(List1);                //displaying list1
   printf("\\nList2=");
   print(List2);               //displaying list1
   
  Intersection_List=intersection_fun(List1,List2);
  printf("\nIntersection_list=");
  
   print(Intersection_List);       //displaying list1
     
    Union_List=union_fun(List1,List2);
printf("\nunion_list=");
   print(Union_List);
}

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.