Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Offline_batch@
#include<stdio.h> //preprocessor
#include<stdbool.h>
#include<malloc.h>
typedef struct node //defining a structure node
{
int num; //declare two variables
struct node *p;
}node;
typedef struct node2 //defining a structure node2 for second list
{
int num2; //declare two variables
struct node2 *p2;
}node2;
node *insert(node *head_ref, int n ); //function for inserting array
void display(node *head_ref); //function for display array
static node *head ,*tail,*newnode,*intersec; //static variables for hold address
static node2 *head2 ,*tail2,*newnode2;
static node *headu ,*tailu,*newnodeu;
void unin(node* head_ref,node *headu_ref );
void intersec_node(node *head_ref,node *head2_ref);
static i=1;
int main()
{
int nos=0,item=0,nos2=0,item2=0;
printf("\nEnter No.Of.Elements in list 1:"); //get input from user
scanf("%d",&nos);
printf("Enter Elements of list 1:\n");
for (int i=0;i<nos;i++)
{
fflush(stdin);
scanf("%d",&item);
head = insert(head,item); //call insert function for insertion
}
printf("Enter No.Of.Elements in list 2:"); //get input from user for second node list
scanf("%d",&nos);
printf("Enter Elements of list 2:\n");
for (int i=0;i<nos;i++)
{
fflush(stdin);
scanf("%d",&item2);
head2 = insert(head2,item2); //call insert function for insertion
}
display(head); //display list 1 elements by calling display function
display(head2);// calling display() f for print elements of list 2
unin(head,head2); // calling unin() for perform union of two list
printf("\n Union of list stored in list3\n ");
display(newnodeu); // prints the union list
printf("\n Intersection of list stored in list 4\n ");
intersec_node(head,head2); // calling intersec_node() fn for perform Intersection of two list
display(intersec); // prints the Intersection list
}
node *insert(node *head_ref,int n) //defining insert function
{
newnode = (node*)malloc(sizeof(node)); //create newnode using malloc
newnode->num=n; //storing the elements
newnode->p=head_ref;
head_ref=newnode;
return head_ref; //returning last address as head
}
void display(node *head_ref) //defining display
{
if(head_ref==0) // if null prints empty
printf("\nList empty...");
else
{
tail=head_ref; // storing in temp variable
printf("\nElements of list %d --> ",i);
++i;
while (tail!=0)
{
printf(" %d",tail->num); //prints all elements
tail=tail->p;
}
}
printf("\n");
}
void unin(node* head_ref,node *headu_ref) //defining unin function
{
newnodeu = NULL; // union elements will be stored in this list
while (head_ref != NULL) // loop run until last node
{
newnodeu=insert(newnodeu,head_ref->num); //elements are inserted in the newnodeu list
head_ref=head_ref->p; // address set to next adress
}
while(headu_ref != NULL)
{
if(!comp(newnodeu,headu_ref->num)) // by using return value of comp function the elements inserted
newnodeu = insert(newnodeu,headu_ref->num); //element inserted if comp func return 0
headu_ref = headu_ref->p; // address set to next adress to check further
}
}
int comp(node *head_ref,int key) // defining comp function
{
node *cmp = head_ref; //stored in local variable
while( cmp != NULL) // loop run until last node
{
if(cmp->num == key) //check list2 element with all elements of newlist
return 1;
cmp = cmp->p; // address set to next adress for comparision
}
return 0;
}
void intersec_node(node *head_ref,node *head2_ref)
{
intersec = NULL; //declaring the list as NULL at initial
while(head2_ref != NULL) // loop run until last node
{
if(comp(head_ref,head2_ref->num)) // by using return value of comp function the elements inserted
intersec = insert(intersec,head2_ref->num);
head2_ref = head2_ref->p; // address set to next node adress
}
}