Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
REPORT PROJECT01 M03
AIM
1) In this project, interfaces for mathematical Set Theory are implemented using C programming language.
2) User can give the input like two list of data and this program will give the output as the union and intersection
of these two lists.
3) Creating two list of data using linked list method.
4) Insert the node to 1st and 2nd list accordingly.
5) Intersection of data of two lists.
6) Getting Union of two list.
PROGRAM
/*
* prac_50.c
*
* Created on: 28-Jul-2022
* Author: RA
*/
#include<stdio.h>
#include<stdlib.h>
typedef struct node1
{
int data;
struct node1 *next;
}node;
/*given 'node' name to created
* data type
*/
void print(node *p);
/*func to print linked list*/
void push(node **head_ref, int new_data);
/*func to push values in given linked list*/
int present(node *head, int data);
/*func to find if passed int is present in
* linked list
*/
node* getinter(node *head1, node *head2);
/*to find intersection of two linked lists*/
node* getuni(node *head1, node *head2);
/*func to find union of two linked lists*/
/*main func*/
int main()
{
int n,m,i,a;
node *p, *q, *head1, *head2;
/*declared ptrs of node type*/
node *intersection= NULL;
node *union_1= NULL;
printf("enter number of nodes in list1: ");
fflush(stdout);
scanf("%d",&n);
/*taken no of nodes for list1*/
printf("enter element 1: ");
fflush(stdout);
scanf(" %d",&a);
/*taken int in head from user*/
p= (node *)malloc(sizeof(node));
p->data= a;
p->next= NULL;
/*created head1 and stored int 'a'
* in the data of node 'p'
* that is head1
*/
head1= p;
/*assigned head1 as p so that
* p can be free and next node
* can be assigned to 'p'
*/
/*creating list1*/
for(i=1;i<n;i++)
/*this loop will create all remaining
* nodes and also take data from user
* and store in that node and
* assign p to next node
*/
{
printf("enter element %d: ",i+1);
fflush(stdout);
scanf(" %d",&a);
q= (node *)malloc(sizeof(node));
q->data= a;
q->next= NULL;
p->next= q;
/*'q' is assigned to next of 'p'
* so that q can free to point to next
* node
*/
p= p->next;
/*in this line p is assigned to
* next node
*/
}
printf("\n");
/*creating list2*/
printf("enter number of nodes in list2: ");
fflush(stdout);
scanf("%d",&m);
printf("enter element 1: ");
fflush(stdout);
scanf("%d",&a);
p= (node *)malloc(sizeof(node));
p->data= a;
p->next= NULL;
head2= p;
for(i=1;i<m;i++)
{
printf("enter element %d: ",(i+1));
fflush(stdout);
scanf(" %d",&a);
q= (node *)malloc(sizeof(node));
q->data= a;
q->next= NULL;
p->next= q;
p= p->next;
}
print(head1);
printf("\n");
print(head2);
printf("\n");
intersection= getinter(head1, head2);
/*called func to find intersection*/
printf("intersection is: ");
print(intersection);
printf("\n");
union_1= getuni(head1, head2);
/*called func to find union*/
printf("union is: ");
print(union_1);
printf("\n");
return 0;
}
/*func to print lists*/
void print(node *p)
{
printf("\n");
while(p!=NULL)
{
printf("\t %d",p->data);
p=p->next; //p is incremented//
}
}
void push(node **head_ref, int new_data)
/*it will put given data in a linked list*/
{
node *new_node= (node *)malloc(sizeof(node));
/*created a new node to store new data*/
new_node->data= new_data;
/*access data from new_node and stored
* new data in that node
*/
new_node->next= (*head_ref);
/*accessed next of new_node and made
* equal to the stored addr of next node
*/
(*head_ref)= new_node;
/*jumped *head_ref to new node */
}
// function that returns true if data is
//present in linked list else return false
int present(node *head, int data)
/*in this func we give the head ptr as
* func arg and int data also as func
* arg
* it returns 1 if given int is present in the given
* linked list
* it returns 0 if int is not
* present in given linked list
*/
{
node *t= head;
while (t!= NULL) //checks for all list
{
if (t->data== data)
/*if given int is present in
* passed linked list return1
*/
{
return 1;
}
t= t->next;
}
return 0;
}
node* getinter(node *head1, node *head2)
{
node *result= NULL;
/*created new linked list of
* node type to store intersection
* result
*/
node *t1= head1;
/*we created t1 to access head1*/
while (t1!= NULL)
/*in this while loop we calling
* func 'present' to check if
* passed data of head1 us
* present in head2
*/
{
if (present(head2, t1->data))
{
push(&result, t1->data);
/*if data is present in head2
* it is pushed to result linked
* list hence push func is used
*/
}
t1= t1->next;
/*t1 is incremented*/
}
return(result);
}
/*func to get union of two linked lists*/
/*we pass two linked lists as arg to func*/
node* getuni(node *head1, node *head2)
{
node *result= NULL;
node *t1= head1, *t2= head2;
while (t1!= NULL)
/*in this loop we pushed all head1
* linked list1 in 'result'
*/
{
push(&result, t1->data);
t1= t1->next;
}
while (t2!= NULL)
/*in this loop we pushed data
* in result which is unique
* in list2
*/
{
if (!present(result, t2->data))
{
push(&result, t2->data);
}
t2= t2->next;
}
return(result);
}
OUTPUT


CONCLUSION
We studied and built required programs and observed output.