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 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

Ans.

#include <stdio.h>
#include <stdlib.h>
// Link list node
typedef struct Node {
int data;
struct Node* next;
}node;
//function declaration
void insert_node(struct Node** head_ref, int n);
int display_list(struct Node* head, int data);
// Function to get union of two linked lists head_pointer1 and head_pointer2
struct Node* get_union(struct Node* head_pointer1, struct Node* head_pointer2)
{
struct Node* result = NULL;
struct Node *n1 = head_pointer1, *n2 = head_pointer2;
// Insert all elements of lisn1 to the result list
while (n1 != NULL)
{
insert_node(&result, n1->data);
n1 = n1->next;
}
// Insert those elements of lisn2 which are not present in result list
while (n2 != NULL)
{
if (display_list(result, n2->data))
{
insert_node(&result, n2->data);
}
n2 = n2->next;
}
return result;
}
// Function to get intersection of two linked lists head_pointer1 and head_pointer2
struct Node* get_intersection(struct Node* head_pointer1, struct Node* head_pointer2)
{
struct Node* result = NULL;
struct Node* n1 = head_pointer1;
// Traverse lisn1 and search each element of it in lisn2. If the element is present in list 2, then insert the element to result
while (n1 != NULL)
{
if (display_list(head_pointer2, n1->data))
{
insert_node(&result, n1->data);
}
n1 = n1->next;
}
return result;
}
// A utility function to insert a node at the beginning of a linked list
void insert_node(struct Node** head_ref, int n)
{
// allocate node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
// put in the data
new_node->data = n;
// link the old list off the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
// function to print a linked list
void printList(struct Node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
// function that returns true if data is present in linked list else return false
int display_list(struct Node* head, int data)
{
struct Node* t = head;
while (t != NULL)
{
if (t->data == data)
{
return 1;
}
t = t->next;
}
return 0;
}
// Driver program to test above function
int main()
{
int n,m,temp;
printf("Enter the Value of link list-1:-");
fflush(stdout);
scanf("%d", &n);
printf("Enter the value of link list-2:-");
fflush(stdout);
scanf("%d", &m);
// Starth structure pointer with the empty list
struct Node* head_pointer1 = NULL;
struct Node* head_pointer2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
//for loop for link lisn1
for(int i=0;i<n;i++)
{
printf("Enter the value in link list-1 %d:-",i);
fflush(stdout);
scanf("%d", &temp);
insert_node(&head_pointer1, temp);
}
//for loop for link lisn2
for(int i=0;i<m;i++)
{
printf("Enter the value in link list-2 %d:-",i);
fflush(stdout);
scanf("%d", &temp);
insert_node(&head_pointer2, temp);

}
//call the function of intersection
intersecn = get_intersection(head_pointer1, head_pointer2);
//call the function of union
unin = get_union(head_pointer1, head_pointer2);
printf("\n First list is ");
//function calling with structure pointer as argument
printList(head_pointer1);
printf("\n Second list is ");
printList(head_pointer2);
printf("\n Intersection list is ");
printList(intersecn);
printf("\n Union list is ");
printList(unin);
return 0;
}

Output:

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.