Challenge

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 (PREETI)

#include<stdio.h>
#include<stdlib.h>
//link list node
typedef struct Node
{
int Value;
struct Node *next;
}Node;

static Node *h1; //storing the address of first linked list
static Node *h2; //storing the address of second linked list
static Node *Head;
static Node *Tail;
static Node *Union;// used for union operation
static Node *intersection;//used for intersection operation
static Node *Result_union;//getting the union result
static Node *Result_intersection;//getting the intersection result
static Node *Headnode;
static Node *Newnode;
//Function declaration
Node *Insert( Node *Headnode , int n);
void Display_node(Head);
Node *get_union( Node *h1,Node *h2);
Node *get_intersection( Node *h1, Node *h2);
int compare( Node *Headnode,int n);

//main function
int main()
{
int a; //This variable defines total number of value in first node
int b; //This variable defines total number of value in second node
int c;//This variable defines the element in first node
int d;//This variable defines the element in second node
int i;
printf("\nEnter total number of value in first node :");
fflush(stdout);
scanf("%d", &a);

printf("\nEnter total number of Value in second node :");
fflush(stdout);
scanf("%d", &b);

//for loop for 1st node
for(i=0;i<a;++i)
{
printf("\nEnter the elements of the 1st node :");
fflush(stdout);
scanf("%d", &c);
h1=Insert(h1,c);
}
Display_node(h1);
for(int i=0;i<b;++i)// //for loop for 2nd node
{
printf("\nEnter the elements of the 2nd node : ");
fflush(stdout);
scanf("%d",&d);
h2=Insert(h2,d);
}
Display_node(h2);
//calling union function
Result_union=get_union(h1,h2);
printf("\n Union of linked list is :");
Display_node(Result_union);
//calling the intersection function
Result_intersection=get_intersection(h1,h2);
printf("\n intersection of linked list is :");
Display_node(Result_intersection);
}

//function to insert a node at the beginning of a linked list
Node *Insert( Node *Headnode , int n)
{
Newnode=(Node*)malloc(sizeof(Node));//allocating node
Newnode->Value = n;//putting the data
Newnode->next = Headnode;//link the old list of new node
Headnode = Newnode;//move the head to point to the new node
return Headnode;
}
//Function to print all linked list
void Display_node(Head)
{

if (Head==NULL)
{
printf("nList is empty");
}
else
{
Tail=Head;
while(Tail != NULL)
{
printf("\n %d",Tail -> Value);
Tail = Tail->next;
}
}
}
//Function to get union of two linked lists head1(h1) and head2(h2)
Node *get_union( Node *h1, Node *h2)
{
while(h1 != NULL)
{
Union=Insert(Union,h1->Value);
h1= h1 ->next;
}
while(h2 != NULL)
{
if(!(compare(Union,h2->Value)))
{
Union=Insert(Union,h2 -> Value);
}
h2 = h2->next;
}
return Union;
}
//Function to get intersection of two linked lists head1(h1) and head2(h2)
Node *get_intersection( Node *h1, Node *h2)
{
while(h1 != NULL)
{
if(compare(h2,h1->Value))
{
intersection=Insert(intersection,h1 -> Value);
}
h1 = h1->next;
}
return intersection;
}
//function to compare
int compare( Node *Headnode,int n)
{
while (Headnode != NULL)
{
if((Headnode->Value) == n)
{
return 1;
}
Headnode=Headnode->next;
}
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.