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>
#include<stdlib.h>//preprocessors
typedef struct node
{
int data;
struct node *next;
}Node;//creating a node
Node* insert_list(Node *head_ref,int data);
void display_list(Node *head_ref);
void union_node(Node *head1_ref,Node *head2_ref);
void intersec_node(Node *head1_ref,Node *head2_ref);
int compare(Node *head_ref,int dada);//sub function declaration
static Node *head1, *head2, *union_list, *intersec, *tail, *newnode, *nextnode;//declaring structure variable globally
int main()//main function
{
int node_nos=0, node_tems=0, del_node=0, data;//local variable declaration
printf("\nEnter the number of nodes in linked list 1: ");
scanf("%d",&node_nos);//input from user for no.of nodes in linked list1
printf("\n");
for(int i=1;i<=node_nos;i++)
{
printf("Enter node data : ");
scanf("%d",&data);
head1 = insert_list(head1,data);
}//get items of list1 from user
printf("\nEnter the number of nodes in linked list 2: ");
scanf("%d",&node_nos);//input from user for no.of nodes in linked list2
printf("\n");
for(int i=1;i<=node_nos;i++)
{
printf("Enter node data : ");
scanf("%d",&data);
head2 = insert_list(head2,data);
}//get items of list2 from user
printf("\nLinked List 1 : \n");
display_list(head1);//display list 1
printf("\nLinked List 2 : \n");
display_list(head2);//display list 2
printf("\nUnion of lists : \n");
union_node(head1,head2);//calling union sub function
display_list(union_list);//display union of lists
printf("\nIntersection of lists : \n");
intersec_node(head1,head2);//calling intersection sub function
display_list(intersec);//display intersection of list
}
Node* insert_list(Node *head_ref,int data) //function to insert a node.
{
nextnode = head_ref;
newnode =(Node*) malloc(sizeof(Node)); //create new node
newnode->data = data;
newnode->next = nextnode;//insert the new node
nextnode = newnode;
return nextnode;
}
void display_list(Node *head_ref) //sub function to display the node in the list.
{
printf("\n");
if(head_ref == 0)
printf("List is empty");
else
{
tail = head_ref;
while(tail != 0)
{
printf(" -> %d",tail->data);//prints each data one by one in loop
tail = tail->next;
}
}
printf("\n");
}
void union_node(Node *head1_ref,Node *head2_ref)//union of list function
{
union_list = NULL;
while(head1_ref != NULL)
{
union_list = insert_list(union_list,head1_ref->data);
head1_ref = head1_ref->next;
}//copy the list1 data to union list
while(head2_ref != NULL)
{
if(!compare(union_list,head2_ref->data))
union_list = insert_list(union_list,head2_ref->data);
head2_ref = head2_ref->next;
}//check the data of union list with list two if not equal insert that in union list
}
void intersec_node(Node *head1_ref,Node *head2_ref)//intersection of lists function
{
intersec = NULL;
while(head2_ref != NULL)
{
if(compare(head1_ref,head2_ref->data))
intersec = insert_list(intersec,head2_ref->data);
head2_ref = head2_ref->next;
}//check the data of two lists if its equal insert the data to intersection list
}
int compare(Node *head_ref,int key)//sub function used to compare in union and intersection
{
Node *cmpr = head_ref;
while( cmpr != NULL)
{
if(cmpr->data == key)//compares the data in the given list with the given key
return 1;
cmpr = cmpr->next;
}
return 0;
}