Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

Project 1:
Topic:
Creation of user-defined data type to implement the user interfaces for working with ‘Set’
(Mathematical Set theory) using Linked List
--------------------------------------------------

//------------libraries------------------
#include<stdio.h>//preprocessor
#include<stdlib.h>

//--------------structure decleration------------------
typedef struct node
{
int data;
struct node *next;
}node;

//------------------structure variables---------------------
static struct node *h1;
static struct node *h2;
static struct node *head;
static struct node *tmp;
static struct node *union_list;
static struct node *intersection;
static struct node *result_union;
static struct node *result_intersection;
static struct node *headnode;
static struct node *newnode;


//---------------------Insert function-------------------

node *insert_fun(node *headnode,int n)
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=n;
newnode->next=headnode;
headnode=newnode;
return headnode;
}

//------------------Display Function-----------------------
void display_list(node *head)
{
if(head==0)
{
printf("Empty List...!\n");
}
else
{
tmp=head;
printf("The values in the list is ...\n");
while(tmp!=0)
{
printf("%d\n",tmp->data);
tmp=tmp->next;
}
}
}

//---------------------unoin--------------------------
node *union_fun(node* head1,node* head2)
{
while(head1!=NULL)
{
union_list=insert_fun(union_list,head1->data);
head1=head1->next;
}
while(head2!=NULL)
{
if(!compare_fun(union_list,head2->data))
{
union_list=insert_fun(union_list,head2->data);
}
head2=head2->next;
}
return union_list;
}

//------------------intersection----------------------
node *intersection_fun(node* head1,node* head2)
{
while(head1!=NULL)
{
if(compare_fun(head2,head1->data))
{
intersection=insert_fun(intersection,head1->data);

}
head1=head1->next;
}
return intersection;
}
//----------------compare-------------------------
int compare_fun(node *headnode,int n)
{
while(headnode!=NULL)
{
if(headnode->data == n)
{
return 1;
}
headnode=headnode->next;
}
return 0;
}

//-------------------------main------------------------------
int main()
{
int n1,n2,first_list_value,second_list_value,i,j;
printf("Enter the total nodes for linked list 1\n");
fflush(stdout);
scanf("%d",&n1);

printf("Enter the total nodes for linked list 2\n");
fflush(stdout);
scanf("%d",&n2);

printf("\nEnter the items\n");
fflush(stdout);
for(i=0;i<n1;i++)
{
scanf("%d",&first_list_value);
h1=insert_fun(h1,first_list_value);
}
printf("The items in list 1 are:\n");
display_list(h1);

printf("\nEnter the items\n");
fflush(stdout);
for(j=0;j<n2;j++)
{
scanf("%d",&second_list_value);
h2=insert_fun(h2,second_list_value);
}
printf("The items in list 2 are:\n");
display_list(h2);


printf("The values after union operation are:\n");
result_union= union_fun(h1,h2);
display_list(result_union);

printf("The values after intersection operation are:\n");
result_intersection = intersection_fun(h1,h2);
display_list(result_intersection);

return 0;
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.