Challenge

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

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

Ans -

1 - In this project are to build the code to creat two list .

2- When created two list it is find the intersecn of list and union of the list.

3 - The finding the unoin of list we are required to set the unin function. This function are to use the union of two list.

4 - The find intersen of two list we use in intesen function to find intersecn of the list.

5 - So we use in struct node to given the data as two list with no of data in node .

6 - The set the main code to display and call the list with data and display thier union and itersen of the list.

code -

1 - Main function.

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

2 - Creat the node to set the data.

struct node
{
int data;
struct node*next;
};

3 - To set decler the i and and create list in node .

int i ;

Inset the list in node.
struct node*createlist()
{
int n; - to use the set no of data in list.Set the header pointer.
struct node*head=NULL,*p,*ptr;
int x,i;

To print the no of element in list.
printf("enter no of elements in list :\n");

To take the and shown in output the give set to printf , saan the given input and stored the n veriable.
fflush(stdout);
scanf("%d",&n);

The scan no of all element so we are set the for loop.

for(i=0;i<n;i++)
{
printf("enter the data value:");
fflush(stdout);
scanf("%d",&x);
p=(struct node*)malloc(sizeof(struct node));
p->data=x;
p->next=NULL;

The set the condition to if and esle statment . Whent head is null to pointer are store in p but it no excute condition excute the else condition.

if(head==NULL)
{
head=ptr=p;

}
else
{
ptr->next=p;
ptr=p;
}

The condition excute its return the head.
}
return head;

}

To set void displaylist to diplay the list data .
void displaylist(struct node*list)
{
struct node*p;
p=list;
while(p!=0)
{
printf("%d\t",p->data); - display the list data.
p=p->next;
}

}

To set the intersecn the stored list 1 and 2 in node using pointer.
int intersecn(struct node*list1,struct node*list2)
{

To print the intersen in list.
printf("\n Intersection list:\t");
fflush(stdout);

to find the intersecn in the two list.
struct node*temp3=list1;
struct node*temp4=list2;
while(temp3)
{
while(temp4)
{
if(temp3->data==temp4->data)
{
printf("%d\t",temp3->data);

}
temp4=temp4->next;
}
temp3=temp3->next;
temp4=list2;
}
return 0;

}

To find the mathmaticall uinion so set the unin function to list 1 and 2 .
int unin(struct node*list1,struct node*list2)
{

Print in the union of the list.
printf("\n union list :\t");
fflush(stdout);

display the result in union of the two list.To set the condition to find the union of list
struct node*temp3=list1;
struct node*temp4=list2;
int flag=1;
displaylist(temp3);
while(temp4)
{
if(temp3->data!=temp4->data)
{
printf("%d\t",temp4->data);

}
temp4=temp4->next;

}
while(temp4)
{
while(temp3)
{
if(temp3->data==temp4->data)
{
flag=0;
}
temp3=temp3->next;
}
if(flag)
{
printf("%d\t",temp3->data);
}
temp4=temp4->next;
temp3=list1;
}

after excute condtion it return it.
return 0;
}

int main are to use in the display the list 1 and 2 and calling the list 1 and 2 in main function.

int main()
{
struct node*list1,*list2;
list1=list2=NULL;

display the list 1 and creat the list .
printf("list1 :\n");
list1=createlist();

display the list 2 and creat the list 2

printf("list2 :\n");
list2=createlist();

calling list 1 and 2

printf("\n list 1 is :");
displaylist(list1);

printf("\n list 2 is :");
displaylist(list2);

display the intersecn and union of list 1 and 2.

intersecn(list1,list2);
unin(list1,list2);

}

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

struct node
{
	int data;
	struct node*next;
};
int i ;
 struct node*createlist()
 {
	 int n;
	 struct node*head=NULL,*p,*ptr;
	 int x,i;
	 printf("enter no of elements in list :\n");
	 fflush(stdout);
	 scanf("%d",&n);

	 for(i=0;i<n;i++)
	 {
		 printf("enter the data value:");
		 fflush(stdout);
		 scanf("%d",&x);
		 p=(struct node*)malloc(sizeof(struct node));
         p->data=x;
         p->next=NULL;
         if(head==NULL)
         {
        	 head=ptr=p;

         }
         else
         {
        	 ptr->next=p;
        	 ptr=p;
         }
	 }
	 return head;

 }
 void displaylist(struct node*list)
 {
	 struct node*p;
	 p=list;
	 while(p!=0)
	 {
		 printf("%d\t",p->data);
		 p=p->next;
	 }

 }
 int intersecn(struct node*list1,struct node*list2)
 {
	 printf("\n Intersection list:\t");
	 fflush(stdout);
	 struct node*temp3=list1;
	 struct node*temp4=list2;
	 while(temp3)
	 {
		 while(temp4)
		 {
			 if(temp3->data==temp4->data)
			 {
				 printf("%d\t",temp3->data);

			 }
			 temp4=temp4->next;
		 }
		 temp3=temp3->next;
		 temp4=list2;
	 }
	 return 0;

 }
 int unin(struct node*list1,struct node*list2)
		{
	 printf("\n union list :\t");
	 fflush(stdout);
	 struct node*temp3=list1;
	 struct node*temp4=list2;
	 int flag=1;
	 displaylist(temp3);
	 while(temp4)
	 {
		 if(temp3->data!=temp4->data)
		 {
			 printf("%d\t",temp4->data);

	 }
	 temp4=temp4->next;

		}
	 while(temp4)
	 {
		 while(temp3)
		 {
			 if(temp3->data==temp4->data)
			 {
				 flag=0;
			 }
			 temp3=temp3->next;
		 }
		 if(flag)
		 {
			 printf("%d\t",temp3->data);
		 }
		 temp4=temp4->next;
		 temp3=list1;
	 }
	 return 0;
		}
 int main()
 {
	 struct node*list1,*list2;
	 list1=list2=NULL;


	 printf("list1 :\n");
	 list1=createlist();

	 printf("list2 :\n");
	 list2=createlist();

	 printf("\n list 1 is :");
	 displaylist(list1);

	 printf("\n list 2 is :");
	 displaylist(list2);

	 intersecn(list1,list2);
	 unin(list1,list2);

 }

Output -

We are set 5 element in list and find thier interscen and union of 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.