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 12 student:
PROJECT 1: Linked lists
AIM:
In this project, interfaces for mathematical Set Theory are implemented using C programming language. 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
Input:-
/*
* project1.c
*
* Created on: 14-Oct-2022
* Author: adity
*/
/*this program is to find intersection and
* union of two LL
*/
#include
#include
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;idata= 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;idata= 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:-
