Challenge

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

main.c


#include"linked_list.h"

int main()
{
NODE* head1 = NULL;
NODE* head2 = NULL;
NODE* intersection =NULL;
NODE* union_list = NULL;


inSert(&head1,60);
inSert(&head1,48);
inSert(&head1, 23);
inSert(&head1,34);

inSert(&head2,20);
inSert(&head2,23);
inSert(&head2, 48);
inSert(&head2,4);


intersection=Intersection(head1,head2);

union_list=Get_Union(head1,head2);


// first list
printf("first list \n");
display(head1);

// second list

printf("second list\n");
display(head2);

// intersection
printf("intersection list \n");
display(intersection);

//uinion list
printf("union list\n");
display(union_list);


return 0;
}

linked_list.h


#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>

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


// function to insert a node in(front) of the linked list
void inSert(NODE **head_ref, int new_data);
//function to check if the value is present inside the linked list
bool checking(NODE *head, int data);
//function to get the union of the list
NODE *Get_Union(NODE *head1, NODE *head2);
//function to get the intersection of two linked list
NODE *Intersection(NODE *head1, NODE *head2);
// function to print the list
void display(NODE *head);


linked_list.c


#include"linked_list.h"


// function to insert a new node in the linked list
void inSert(NODE ** head_ref, int new_data)
{
NODE * new_node = (NODE*)malloc(sizeof(NODE));
new_node->value = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;

}


// function to check if the data is matching or not
bool checking(NODE *head, int data)
{
NODE *TEMP = head;
while(TEMP != NULL)
{
if(TEMP->value == data)
{
return 1;
}
TEMP= TEMP->next;
}
return 0;
}


// function to get the union
NODE *Get_Union(NODE* head1,NODE* head2)
{

NODE*result=NULL;

NODE *temp1 = head1, *temp2 = head2;


while(temp1 != NULL)
{
inSert(&result, temp1->value);
temp1=temp1->next;
}

while(temp2 != NULL)
{
if(!checking(result,temp2->value))
{
inSert(&result,temp2->value);
}
temp2=temp2->next;
}

return result;
}


// function to get the intersection

NODE * Intersection(NODE *head1,NODE * head2)
{

NODE *result =NULL;
NODE * temp = head1;

while(temp != NULL)
{
if(checking(head2, temp->value))
{
inSert(&result,temp->value);
}
temp = temp->next;
}


return result;

}


//function to display the linked list

void display(NODE * head)
{

while(head != NULL)
{
printf("%d\n", head->value);
head= head->next;
}
}

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.