Challenge

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

Ans:

/*
* Linkedlist.c
*
* Created on: Sep 10, 2022
* Author: SUSHMA
*/
#include <stdio.h>
#include <stdlib.h>


typedef struct
{
int value;
struct node *next;
}node;

static node *h1,*h2,*head,*tail,*Union,*intersection,*result_union,*result_intersection, *headnode,*newnode;

node *insert(node *headnode,int n);
void Display(node *head);
node *Union_fun(node *head1,node *head2);
node *intersection_fun(node *head1,node *head2);
int comparison(node *headnode,int n);


int main()
{
int total_first; //toal number of first node
int total_second;//toal number of second node
int first_list_value;//first list items
int second_list_value;//second list items
printf("Enter the total nodes of first list:");
fflush(stdout);
scanf("%d",&total_first);

printf("Enter the total nodes of second list:");
fflush(stdout);
scanf("%d",&total_second);

int i;
for(i=0;i<total_first;i++)
{
printf("Enter the first list value %d:",i);
fflush(stdout);
scanf("%d",&first_list_value);
h1=insert(h1,first_list_value);//call insert function
}
Display(h1);//Display first list value
printf("\n");
for(i=0;i<total_second;i++)
{
printf("Enter the second list value %d:",i);
fflush(stdout);
scanf("%d",&second_list_value);
h2=insert(h2,second_list_value);//call insert function
}
Display(h2);//Display second list value

result_union=Union_fun(h1,h2);//returning head pointers of Union
printf("\nDisplay result_Union:");
fflush(stdout);
Display(result_union);

result_intersection=intersection_fun(h1,h2);
printf("\n Display result_inersection:");
fflush(stdout);
Display(result_intersection);

return 0;
}

node *insert(node *headnode,int n)
{
newnode=(node*)malloc(sizeof(node));
newnode->value=n; //n is stored in value
newnode->next=headnode;//address of headnode is assigned to newnode->next
headnode=newnode;//address of new node is assigned to headnode
return headnode;//return address of head node

}

void Display(node *head)
{
if(head==NULL)
{
printf("empty list");
}
else
{
tail=head;
while(tail!=0)
{
printf("%d ",tail->value);
tail=tail->next;
}

}
}

node *Union_fun(node *head1,node *head2)
{
while(head1 != NULL)
{
Union=insert(Union,head1->value);
head1=head1->next;//incrementing head1
}

while(head2 != NULL)
{
if(comparison(Union,head2->value) != 1)
{
Union=insert(Union,head2->value);
}
head2=head2->next;//incrementing head2
}
return Union;
}

Node *intersection_fun(Node *head1,Node *head2)
{
while(head1 != NULL)
{
if(comparison(head2,head1->value)==1)
{
intersection=insert(intersection,head1->value);
}
head1=head1->next;

}
return intersection;

}

int comparison(node *headnode,int n)
{
while(headnode != NULL)
{
if(headnode->value==n)
{
return 1;
}
headnode=headnode->next;
}
return 0;
}

Result:

Enter the total nodes of first list:5
Enter the total nodes of second list:4
Enter the first list value 0:1
Enter the first list value 1:2
Enter the first list value 2:3
Enter the first list value 3:4
Enter the first list value 4:5
5 4 3 2 1
Enter the second list value 0:4
Enter the second list value 1:5
Enter the second list value 2:6
Enter the second list value 3:7
7 6 5 4
Display result_Union:6 7 1 2 3 4 5
Display result_inersection:4 5

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.