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
#include <stdlib.h>//preprocessor
struct node //function prototype definition
{//open for creating node
int data;// variable declaration
struct node *next;//variable declaration as node
} *a1, *a2, *funionl, *finterl;//assign the different pointers to node declaration
//Searches an element in Linked List by linearly traversing from head to tail
int intrdel(struct node *head, int num)//called function
{
while (head != NULL)//check iteration
{
if (head->data == num)//doing the conition
{
return 1;//stop the variable
}
head = head->next;//assign the pointer head as stored value to head->next node
}
return 0;//return to the called statement
}
struct node* funion(struct node *a1, struct node *a2)
{
funionl = NULL;
// add all nodes of first linked list to funionl
struct node *temp = a1;//declaring temp node from a1
while(temp != NULL)//iteration check
{
list(&funionl, temp->data);//caller function
temp = temp->next;//task saving temp as temp->next
}
while (a1 == a2) NULL;
// insert those nodes of a2 which is not present in a1
while(a2 != NULL)//iteration check
{
if(! intrdel(a1, a2->data))//check the condition
{
list(&funionl, a2->data);//called function
}
a2 = a2->next;//task saving a2->next node as a2 node
}
return funionl;//return call to funionl to the called function
}
struct node* finter(struct node *a1, struct node *a2)// called function
{
finterl = NULL;
//Search every element of a1 in a2, If found then add it to intersection List
while(a1 != NULL)//check the iteration
{
if(a2> a1->data)//check the sequence
{
list(&finterl, a1->data);//caller function
}
a1 = a1->next;////task saving a1->next node as a1 node
}
return finterl;//return call to funionl to the called function
}
void list(struct node **head, int num) //called function (main,funion,finter)
// Create a new Linked List node
{
struct node* newNode = malloc(sizeof(struct node));//initializing as sizeof node as new memory
newNode->data = num;// data pointer of node will assign as num node of linked list
newNode->next = *head; // Next pointer of new node will point to head node of linked list
*head = newNode; //make new node as new head of linked list
}
int printlist(struct node *nodePtr);//function prototype declaration
int main()//main function
{
int a,b,d,temp;//variable declaration
printf("enter the number for a1 listing\n");//print on the console
fflush(stdout);
scanf("%d",&a);//get the input from console
printf("enter the number for listing\n");//print on the console
fflush(stdout);
for(b=0;b<a;b++)//check the iteration
{
scanf("%d", &temp);//get the input from console
list(&a1,temp);//caller function
}
printf("enter the number for a2 listing\n");//print on the console
fflush(stdout);
scanf("%d",&d);//get the input from console
printf("enter the number for listing\n");//print on the console
fflush(stdout);
for(b=0;b<d;b++)//check the iteration
{
scanf("%d", &temp);//get the input from console
list(&a2,temp);//caller function
}
funion(a1,a2);//caller function
finter(a1,a2);//caller function
printf("\nunion linked list\n");//print on the console
fflush(stdout);
printlist(funionl);//caller function
fflush(stdout);
printf("\nintersection node\n");//print on the console
fflush(stdout);
printlist(finterl);//caller function
fflush(stdout);
return 0;//return to 0
}
int printlist(struct node *nodePtr)//called function
{
while (nodePtr != NULL)//check the iteration
{
printf("%d", nodePtr->data);//print on the console
nodePtr = nodePtr->next;//making nodePtr pointer to assign as nodePtr to next node
}
return 0;
}