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<stdio.h>//standard input output library

#include<stdlib.h>//library for exit function

/*creating selfrefential structure with structure members

value of int and nextptr of struct node type

*/

struct node

{

int value;

struct node*nextptr;

};

struct node *stnode1,*stnode2,*p;//creating pointer variables of struct node

void find_union(struct node *head1,struct node *head2,int value1,int value2)

{

int arr1[50],arr2[50];//creating two array

int i=0,j,k,c;//creating variables for iterations

int u[100]={0};//creating array to store the result

struct node *tmp1,*tmp2;//creating two pointer variables of struct node type

tmp1=head1;//copying the adress that comes from the argument

tmp2=head2;//copying the adress that comes from the argument

while(tmp1!=NULL)//checking that the pointer exists

{

arr1[i]=tmp1->value;//copying value in linked list to array

tmp1=tmp1->nextptr;//updating the pointer

i++;//incrementing

}

i=0;//initialized to zero for other iteration

while(tmp2!=NULL)//checking that the pointer exists

{

arr2[i]=tmp2->value;//copying value in linked list to array

tmp2=tmp2->nextptr;//updating the pointer

i++;//incrementing

}

for(i=0;i<value1;i++)//for printing the elemnts in list 1

{

u[i]=arr1[i];

}

j=i;//copying the last index value

for(i=0;i<value2;i++)//for loop for checking the element in list 2 with all elemnts of array 1

{

c=0;//for setting a flag varaible

for(k=0;k<value1;k++)

{

if(arr2[i]==u[k])//checking the array1 elments is equal to array2

{

c++;//if yes set flag variable

}

}

if(c==0)//checking flag not found

{

u[j++]=arr2[i];//if not found so no repetation so copy that element
}




}
printf("PRINTING UNION ---");
for(i=0;i<j;i++)
{

printf(" %d",u[i]);//for printing all elements saved in result array
}


}
void find_intersection(struct node *head1,struct node *head2,int value1,int value2)
{
int arr1[50],arr2[50];//creating two array
int i=0,j,k;//creating variables for iterations
int u[100]={0};//creating array to store the result
struct node *tmp1,*tmp2;//creating two pointer variables of struct node type
tmp1=head1;//copying the adress that comes from the argument
tmp2=head2;//copying the adress that comes from the argument
while(tmp1!=NULL)//checking that the pointer exists
{
arr1[i]=tmp1->value;//copying value in linked list to array
tmp1=tmp1->nextptr;//updating the pointer
i++;//incrementing

}
i=0;//initialized to zero for other iteration
while(tmp2!=NULL)//checking that the pointer exists
{
arr2[i]=tmp2->value;//copying value in linked list to array
tmp2=tmp2->nextptr;//updating the pointer
i++;//incrementing

}
k=0;
for(i=0;i<value1;i++)
{

for(j=0;j<value2;j++)
{
if(arr1[i]==arr2[j])
{
u[k]=arr1[i];//for comapiring all value of list 1 with list 2
k++;//incrementing the position of the result array
}
}
}
printf("\nPRINTING INTERSECTION---");
for(i=0;i<k;i++)
{
printf(" %d",u[i]);//printing all the result that stored in the result array
}
}


int main()//main function
{
struct node *tmp,*fnnode;//creating pointer variables of struct node type
int num1,num2,i=0,unit=0;
int choice=0;//for switch input

///////////////////////////////////////////LIST ONE///////////////////////////////////////////////////

printf("ENTER THE NUMBER OF NODES FOR LIST 1");
fflush(stdout);
scanf("%d",&num1);
stnode1=(struct node*)malloc(sizeof(struct node));//allocating memory dynamically
if(stnode1==NULL)//checking the starting node is empty or not
{
printf("MEMORY CANT BE ALLOCATED");
exit(0);//if yes exit

}
else//else enter the 1 st element
{

printf("ENTER THE %d ELEMENT",i+1);
fflush(stdout);
scanf("%d",&unit);
stnode1->value=unit;//strucure varaiable accessing using pointer,giving that elememt
stnode1->nextptr=NULL;//strucure varaiable accessing using pointer,giving the null
tmp=stnode1;//copying the starting node

}
for(i=1;i<num1;i++)//for loop for creating the remaining nodes
{
fnnode=(struct node*)malloc(sizeof(struct node));//allocating memory dynamically
if(fnnode==NULL)//checking if that is null
{
printf("MEMORY CANT BE ALLOCATED");
exit(0);//if null exit 0


}
else//else enter the 2 nd elements onwards
{
printf("ENTER THE %d ELEMENT",i+1);
fflush(stdout);
scanf("%d",&unit);
fnnode->value=unit;//strucure varaiable accessing using pointer ,giving that elememt
fnnode->nextptr=NULL;//strucure varaiable accessing using pointer,giving the null
tmp->nextptr=fnnode;//updating your pointer
tmp=fnnode;//copying the pointer
}

}
//////////////////////////////////////////LIST TWO//////////////////////////////////////////////////

i=0;
printf("ENTER THE NUMBER OF NODES FOR LIST 2");
fflush(stdout);
scanf("%d",&num2);
stnode2=(struct node*)malloc(sizeof(struct node));//dynamic memory allocation
if(stnode2==NULL)//checking if that null or not
{
printf("MEMORY CANT BE ALLOCATED");
exit(0);//if null exit

}
else//else enter the 1st element
{
printf("ENTER THE %d ELEMENT",i+1);
fflush(stdout);
scanf("%d",&unit);
stnode2->value=unit;//strucure varaiable accessing using pointer ,giving that elememt
stnode2->nextptr=NULL;//strucure varaiable accessing

using pointer,giving the null
tmp=stnode2;//copying the pointer

}
for(i=1;i<num2;i++)
{
fnnode=(struct node*)malloc(sizeof(struct node));//dynamic memory alloaction
if(fnnode==NULL)//checking if that null
{
printf("MEMORY CANT BE ALLOCATED");
exit(0);//if null exit


}
else//else enter the remaining elements
{
printf("ENTER THE %d ELEMENT",i+1);
fflush(stdout);
scanf("%d",&unit);
fnnode->value=unit;//strucure varaiable accessing using pointer ,giving that elememt
fnnode->nextptr=NULL;//strucure varaiable accessing using pointer,giving the null
tmp->nextptr=fnnode;//updating the pointer
tmp=fnnode;//copying the pointer
}


}
fflush(stdout);//for clearing the console
while(1)//infinite loop
{
printf("\nWHAT OPERATION YOU NEED TO PERFORM\n");//display the memu for user
printf(" MENU\n");
printf(" 1)INTERSECTION\n 2)UNION\n 3)EXIT");
fflush(stdout);
scanf("%d",&choice);
switch(choice)//switch
{
case 1:
find_intersection(stnode1,stnode2,num1,num2);//function call-case 1
break;
case 2:
find_union(stnode1,stnode2,num1,num2);//function call-case 2
break;

case 3:
exit(0);//case 3 then exit

default:
printf("INVALID INPUT");//deafult print inavalid
break;
}


}
return 0;//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.