Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
LIVE BATCH 5
CODE:
/*
* 1_project.c
*
* Created on: 17-Sep-2022
* Author: thira
*/
#include<stdio.h> //preprocessor
#include<stdlib.h> //preprocessor
struct node //structure declaration
{
int data;
struct node *next;//members
};
struct node *head1,*head2,*temp1,*temp2;//struct node variables
static void Linked_list1(int x) //function definition
{
if(x==0) //condition check
printf("Link List 1 not available");//print output in console
else
{
struct node *newnode; //struct node variable
head1= malloc(sizeof(struct node));//allocating memory
if(NULL==head1)//if condition
printf("Memory not allocated");//print output in console
else
{
printf("Enter the data of node1:"); //print output in console
fflush(stdout); //clear buffer
scanf("%d",&head1->data); //read input from console
head1->next=NULL;//passing Null to next of head
temp1=head1;//passing address
}
for(int i=2;i<=x;++i){ //for loop
newnode=malloc(sizeof(struct node));//allocating memory
if(NULL==newnode) //if condition
printf("Memory not allocated");//print output in console
else
{
printf("Enter the data of node %d : ",i); //print output in console
fflush(stdout);//clear buffer
scanf("%d",&newnode->data);//read input from console
newnode->next=NULL;//passing Null to next of new node
temp1->next=newnode;//passing address
temp1=newnode;//passing address
}
}
}
}
static void Linked_list2(int y)//function definition
{
if(y==0)//condition check
printf("Not possible to Create Linled-list2");//print output in console
else
{
struct node *newnode;//structure variable
head2=malloc(sizeof(struct node));//allocating memory
if(NULL==head2)//if condition
printf("Memory not allocated");//print output in console
else{
printf("Enter the data of node1:");//print output in console
fflush(stdout);//clear buffer
scanf("%d",&head2->data);//read input from console
head2->next=NULL;//passing Null to next of head
temp2=head2;//passing address
}
for(int i=2;i<=y;++i){ //for loop
newnode=malloc(sizeof(struct node)); //allocating memory
if(NULL==newnode) //if condition
printf("Memory not allocated"); //print output in console
else
{
printf("Enter the data of node %d :",i);//print output in console
fflush(stdout);//clear buffer
scanf("%d",&newnode->data);//read input from console
newnode->next=NULL;//passing Null to next of new node
temp2->next=newnode;//passing address
temp2=newnode;//passing address
}
}
}
}
static void display1(void) //function definition
{
struct node *display1; //structure variable
display1=head1; //passing address
if(display1==NULL) //condition check
printf("\nLink List not available\n"); //print output in console
else
{
printf("Elements in the Linked_list1 are:"); //print output in console
while(display1!=NULL) //while condition
{
printf("%d ",display1->data);//print output in console
display1=display1->next;//passing address
}
printf("\n");//print output in console
}
}
static void display2(void) //function definition
{
struct node *display2; //structure variable
display2=head2; //Passing address
if(display2==NULL) //condition check
printf("\nLink List not available\n"); //print output in console
else
{
printf("Elements in the Linked_list2 are:");//print output in console
while(display2!=NULL) //while condition
{
printf("%d ",display2->data); //print output in console
display2=display2->next;//passing address
}
printf("\n");//print output in console
}
}
static void union_intersection(int x,int y) //function definition
{
if(x==0 || y==0)//condition check
printf("union_and_intersection operations are not possible");//print output in console
else
{
int arr[x+y]; //variable declaration
int i,temp,count=0; //variable declaration and initialization
struct node *Union,*tmp; //structure variables
Union=head1; //passing address
tmp=head2; //passing address
for(i=0;i<x;++i) //for loop
{
arr[i]=Union->data; //passing data to array
Union=Union->next; //passing address
}
for(int j=x;j<(x+y);++j)
{
arr[j]=tmp->data;
tmp=tmp->next;
}
printf("Elements of array are:");
for(int a=0;a<(x+y);++a)
{
printf("%d ",arr[a]);
}
printf("\n");
for(int a=0;a<(x+y);++a)//for loop
{
for(int b=0;b<(x+y);++b) //for loop
{
if(arr[b]>arr[b+1])//if condition
{
//Swapping
temp=arr[b+1];
arr[b+1]=arr[b];
arr[b]=temp;
}
else
continue;//continue to next iteration
}
}
printf("Elements of array after sorting are:");
for(int a=0;a<(x+y);++a)
{
printf("%d ",arr[a]);
}
printf("\n");
printf(" Union of Linked list 1 and 2: ");//print output in console
for(int j=0;j<(x+y);++j)//for loop
{
if(arr[j]==arr[j+1])//condition check
{
continue;//continue to next iteration
}
else
printf("%d ",arr[j]);//print output in console
}
printf("\nIntersection of linked list 1 and 2: ");//print output in console
for(int j=0;j<(x+y);++j)//for loop
{
if(arr[j]==arr[j+1])//condition check
{
printf("%d ",arr[j]);//print output in console
++count;//pre-incrementing count
}
else
continue;//continue to next iteration
}
}
}
int main() //main function
{
int x,y; //variable declaration
printf("Enter the Number nodes in Linked_list1:"); //print output in console
fflush(stdout); //Clear buffer
scanf("%d",&x); //read input from console
printf("Enter the Number nodes in Linked_list2:"); //print output in console
fflush(stdout); //clear buffer
scanf("%d",&y); //read input from console
printf("**Enter data of Linked_list1**\n"); //print output in console
Linked_list1(x); //caller function
display1(); //caller function
printf("**Enter data of Linked_list2**\n"); //print output in console
Linked_list2(y); //caller function
display2(); //caller function
union_intersection(x,y); //caller function
return 0; //return type
}
OUTPUT:
