Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
/*
* project1.c
*
* Created on: 01-May-2022
* Author: kabhi
*/
/* HEADER SECTION */
#include <stdio.h>
#include <stdlib.h>
/* STRUCTURE DEFS */
struct node1
{
int data;
int key;
struct node1 *next;
}A;
struct node2
{
int data;
int key;
struct node2 *next;
}B;
struct node1 *head1, *tail1;
struct node2 *head2, *tail2;
/* STRUCTURE VARIABLES */
void list1(void)
{
head1 = tail1 = NULL;
struct node1 *new; //TO INSERT NODES INTO LL
int i,n,j=0;
printf("ENTER THE SIZE OF THE LIST 1 ");
fflush(stdout);
scanf("%d",&n);
do
{
for(i=0;i<3;i++){
new = (struct node1*) malloc(sizeof(struct node1));
printf("ENTER VALUE FOR NODE %d:" , i+1);
fflush(stdout);
scanf("%d", &new->data);
new->key = i;
if(head1 == NULL)
head1 = tail1 = new;
else
{
tail1->next = new;
tail1 = new;
tail1->next = NULL;
}
j++;
}
}while(j<n);
printf("\nPRINTING LIST 1 ELEMENTS >>");
//printing list1
struct node1 *temp;
temp = head1;
printf("\nLIST 1 ELEMENTS: \n");
while(temp !=NULL){
printf("%d\t", temp->data);
temp = temp-> next;}
printf("\n");
free(temp);
}
void list2 (void)
{
printf("\n\n");
head2 = tail2 = NULL;
struct node2 *new;
int i,n, j=0;
printf("ENTER THE SIZE OF THE LIST 2 ");
fflush(stdout);
scanf("%d",&n);
do
{
for(i=0;i<3;i++)
{
new = (struct node2*) malloc(sizeof(struct node2));
printf("ENTER VALUE FOR NODE %d: ", i+1);
fflush(stdout);
scanf("%d",&new->data);
new->key = i;
if(head2 == NULL)
head2 = tail2 = new;
else
{
tail2->next = new;
tail2 = new;
tail2->next = NULL;
}
j++;
}
}while(j<3);
printf("\nPRINTING LIST 2 ELEMENTS>>>");
struct node2 *temp;
temp = head2;
printf("\nLIST 2 ELEMENTS: \n");
while(temp != NULL){
printf("%d\t", temp->data);
temp= temp-> next;
}
printf("\n");
free(temp);
}
void setInter(void)
{
int out[6];
int i, k =0;
struct node2 *temp2;
struct node1 *temp1;
temp1 = head1;
temp2 = head2;
for(i=0;i<3;i++)
{
while(temp2 !=NULL){
if(temp1->data == temp2->data){
out[k] = temp2->data;
k++;
}
temp2=temp2->next;}
temp1 = temp1->next;
temp2 = head2;
}
//printing_intersection
printf("\nSET INTERSECTION: ");
for(i=0;i<k;i++)
printf("%d\t",out[i]);
printf("\n");
}
void setUnion(void)
{
int out[6],i;
int k=0;
struct node2* temp2;
struct node1* temp1;
temp1 = head1;
temp2 = head2;
while((temp1 !=NULL) || (temp2 != NULL))
{
if(temp1 == NULL)
{
while(temp2 != NULL)
{
out[k++]= temp2->data;
temp2 = temp2->next;}
}
else
{
out[k++] = temp1->data;
temp1 = temp1->next;
}
}
printf("\nSET UNION: ");
for(i=0; i<k;i++)
printf("\%d\t", out[i]);
}
int main(void)
{
list1();
list2();
setInter();
setUnion();
return 0;
}
OUTPUT:
