Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Aim:
Implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List.
Overview of the project:
In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists.
1.Here we taken two sets of the data A={1,2,3,4,5} and B={2,6,5,7,1,8}
2.After that we can apply the union and intersection of these data.
Goals:
1.Implementation of interface for mathematical Set Theory using C programming.
2.Giving two list of data as input to get the output as union & intersection for given list of data.
3.Unions means it will store the all data in same location.
4.Intersection means it will store same data of both given list.
Code:
// C code implementation of interface for mathematical set theory using linked list
#include<stdio.h> //preprocessor header file
#include<stdlib.h>
// create Link list node
struct Node //define structure node
{
int data;
struct Node* next; // Pointer pointing towards next node
};
int i;
struct Node* createlist()
{
int n; //declaration of integer variable
struct Node *head=NULL,*p,*ptr; //structure pointer
int x,i;
printf("enter no of element in list : \n"); // writes to console
fflush(stdout); //to clear standard out console buffer
scanf("%d",&n); // reads to the console %d format specifier
for(i=0;i<n;i++) //for loop to scanning different values
{
printf("enter the data value: "); // writes to console
fflush(stdout); //to clear standard out console buffer
scanf("%d",&x); // reads to the console %d format specifier
p=(struct Node*)malloc(sizeof(struct Node));
p->data=x;
if(head == NULL)
{
head=ptr=p;
}
else
{
ptr->next=p;
ptr=p;
}
}
return head;
}
//structure to print the linked list
void displaylist(struct Node *list)
{
struct Node *p;
p=list;
while(p != 0)
{
printf("%d",p->data); //print the data of current list
fflush(stdout);
p=p->next; //advance the position of current node
}
}
// Function to get intersection of two linked list list1 &l list2
int intersection(struct Node *list1,struct Node *list2)
{
printf("\nIntersection list:\t"); //writes to console
fflush(stdout);
struct Node *temp3=list1;
struct Node *temp4=list2;
while(temp3)
{
while(temp4)
{
if(temp3->data == temp4->data) //if temp3 is equal to temp4, data temp3 is next node
{
printf("%d\t",temp3->data);
}
temp4= temp4->next;
}
temp3= temp3->next;
temp4= list2;
}
return 0;
}
//Function to get union of two linked lists list1 & list2
int unin(struct Node *list1,struct Node *list2)
{
printf("\nUnion list:\t"); //writes to console
fflush(stdout);
struct Node *temp3 = list1;
struct Node *temp4 = list2;
displaylist(temp3);
while(temp4)
{
if(temp3->data != temp4->data) //if temp3 is not equal to temp4, data temp3 is next node
{
printf("%d\t",temp4->data);
}
temp4= temp4->next;
}
return 0;
}
// Main function
int main()
{
struct Node *list1,*list2; // function calling with structure pointer
list1=list2=NULL;
//print and create the list display
printf("list1 :\n");
list1=createlist();
printf("list2 :\n");
list2=createlist();
printf("\nlist1 is: ");
displaylist(list1); //display list1
printf("\nlist2 is: ");
displaylist(list2); //display list2
intersection(list1,list2);
unin(list1,list2);
}
Output:

Conclusion:
We Run these code successfuly completed.
Embedded Live Batch-9