Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Solution:-
#include
#include
struct node {
int data; // Data for the Node
struct node *next; // Address of next Node
}; // Node Declaration
int i; // Global Variable declaratrion
struct node* createlist( )
{
int n; // Local Variable Declaration
struct node *head=NULL,*p,*ptr;
int Value;
printf("Enter no of elements in list : \n"); // Taking value of Number of Node values
fflush(stdout);
scanf("%d",&n);
for(i=0;idata = Value; // Link the Value to Node data
p->next = NULL; // Link the next pointer to Null
if(head == NULL)
{
head = ptr = p;
}
else
{
ptr->next = p;
ptr = p;
}
}
return head;
} // Creating link list
void displaylist(struct node *list)
{
struct node *p;
p = list; // Assgine List value to p.
while(p != 0)
{
printf("%d\t", p->data); // Print the value of Data
p = p->next;
}
} // Print the value of Lists
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) // Compare the data for both link list
{
printf("%d\t", temp3->data); // Printing the value of Intersection List
}
temp4 = temp4->next; // taking next pointer address of list2
}
temp3 = temp3->next; // taking next pointer address of list1
temp4 = list2;
}
return 0;
} // This function Use for the intersection value output
int unin(struct node *list1,struct node *list2)
{
printf("\n Union list:\t");
fflush(stdout);
struct node *temp3 = list1;
struct node *temp4 = list2;
displaylist(temp3); // In this first print all the values of List1
while(temp4)
{
if(temp3->data != temp4->data) // Compare the value in both the list if value is not matched
{
printf("%d\t",temp4->data); // Print the value those not matched
}
temp4 = temp4->next;
}
return 0;
}
int main()
{
struct node *list1,*list2 ;
list1=list2=NULL;
printf("list 1:\n");
list1 = createlist();
printf("list 2:\n");
list2 = createlist();
printf("\n list 1 is : ");
displaylist(list1);
printf("\n list 2 is : ");
displaylist(list2);
intersecn(list1,list2); // Calling Intersection list
unin(list1,list2); // Calling Union list
}
Screenshots:-





Result:-
