Challenge

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

/*
* skilllync.c
*
* Created on: 02-Nov-2022
* Author: B MANOJ KUMAR
*/
#include
#include
typedef struct node
{
int value;
struct node *next;
}Node;

static Node *h1;
static Node *h2;
static Node *head;
static Node *tail;
static Node *union_list;
static Node *intersection;
static Node *result_union;
static Node *result_intersection;
static Node *headnode;
static Node *newnode;

Node *insert(Node *headnode, int n);
void Display_Node(head);
Node *union_node(Node *h1, Node *h2);
Node *intersection_node(Node *h1, Node *h2);
int compare(Node *headnode, int n);

int main()
{
int total_first;
int total_second;
int first_list_value;
int second_list_value;
int i;
printf("nEnter the nodes to be created in 1st node:");
fflush(stdout);
scanf("%d",&total_first);
printf("nEnter the nodes to be created in 2nd node:");
fflush(stdout);
scanf("%d",&total_second);
fflush(stdout);
for(i=0;i<total_first;++i)
{
printf("Enter the elements of the 1st node:");
fflush(stdout);
scanf("%d",&first_list_value);
h1 = insert(h1,first_list_value);
}
Display_Node(h1);
for(i=0;i<total_second;++i)
{
printf("nEnter the elements of the 2nd node:");
fflush(stdout);
scanf("%d",&second_list_value);
h2 = insert(h2,second_list_value);
}
Display_Node(h2);
result_union = union_node(h1,h2);
printf("nUnion of linked list:");
Display_Node(result_union);
result_intersection = intersection_node(h1,h2);
printf("nIntersection of linked list:");
Display_Node(result_intersection);

}

Node *insert(Node *headnode,int n)
{
newnode = (Node *) malloc (sizeof(Node));
newnode->value = n;
newnode->next = headnode;
headnode = newnode;
return headnode;
}

void Display_Node(head)
{
if(head == NULL)
{
printf("n List is empty");
}
else
{
tail = head;
while(tail != NULL)
{
printf("nLinked list values are:%d",tail->value);
tail = tail->next;
}
}
}

Node *union_node(Node *h1, Node *h2)
{
while(h1 != NULL)
{
union_list = insert(union_list,h1->value);
h1 = h1->next;
}
while(h2 != NULL)
{
if(!(compare(union_list,h2->value)))
{
union_list = insert(union_list,h2->value);
}
h2 = h2->next;
}
return union_list;
}

Node *intersection_node(Node *h1, Node *h2)
{
while(h1 != NULL)
{
if(compare(h2,h1->value))
{
intersection = insert(intersection,h1->value);
}
h1 = h1->next;
}
return intersection;
}

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.