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> //preprocessor directive
#include<stdlib.h>
struct node // structure
{
int data; //datatype member
struct node *link;
};
// fun prototype
struct node* insert_node(struct node *head_ref,int data);
void disply_list(struct node *head_ref);
void union_node(struct node *head1_ref,struct node *head2_ref);
void intsec_node(struct node *head1_ref,struct node *head2_ref);
int compare(struct node *head_ref,int data);
static struct node *head1,*head2,*union_list,*intsec,*end, *newnode, *nextnode; // fun pointer
int main() // main fun
{
int node_num=0,data; //variable decl
printf("\nenter the no of node in 1st linked list : ");//no of nodes in 1st list
scanf("%d",&node_num);
for(int i=1;i<=node_num;i++) // for loop insert the data inside the 1st node
{
printf("enter the node data : ");
scanf("%d",&data); // data from user
head1 = insert_node(head1,data); // store the data in list 1
}
printf("\nenter the no of node in 2 nd linked list : ");
scanf("%d",&node_num); //no of nodes in 2nd list
for(int i=1;i<=node_num;i++) // for loop insert the data inside the 2nd node
{
printf("enter the node data : ");
scanf("%d",&data);//data from user
head2 = insert_node(head2,data); // store the data in list 2
}
printf("linked list 1 : \n");
display_list(head1); // sub function call
printf("linked list 2 : \n");
display_list(head2); // sub function call
printf("union in the list : \n");
union_node(head1,head2); // sub fun call for check the union of list
display_list(union_list); // sub fun call for display union list
printf("intersection of the list 1 and 2 : \n");
intsec_node(head1,head2); // sub fun call to insersection the data
display_list(intsec); // sub fun call
}
// fun for insert the new nodes
struct node* insert_node(struct node *head_ref,int data)
{
nextnode = head_ref; // head add store to next node
newnode =(struct node*)malloc(sizeof(struct node)); // create the new node
newnode->data = data; // data into newnode
newnode->link = nextnode; // nextnode add store to newnode link
nextnode = newnode; // newnode store to nextnode
return nextnode;
}
void display_list(struct node *head_ref) // display fun for displaying the datas inside the node
{
if(head_ref == 0)
printf("List is empty");
else
{
end = head_ref;
while(end != 0)
{
printf(" %d\n",end->data);
end = end->link;
}
}
}
void union_node(struct node *head1_ref,struct node *head2_ref) // check the unions
{
union_list = NULL;
while(head1_ref != NULL)
{
union_list = insert_node(union_list,head1_ref->data);
head1_ref = head1_ref->link;
}
while(head2_ref != NULL)
{
if(!compare(union_list,head2_ref->data))
union_list = insert_node(union_list,head2_ref->data); // it compare if it is not equal it add to the list
head2_ref = head2_ref->link;
}
}
void intsec_node(struct node *head1_ref,struct node *head2_ref) // check the intersec of list
{
intsec = NULL;
while(head2_ref != NULL)
{
if(compare(head1_ref,head2_ref->data))
intsec = insert_node(intsec,head2_ref->data); // it will check if it is same it will add to the list
head2_ref = head2_ref->link;
}
}
int compare(struct node *head_ref,int key)
{
struct node *cmpr = head_ref;
while( cmpr != NULL)
{
if(cmpr->data == key)
return 1;
cmpr = cmpr->link;
}
return 0;
}