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 14 EMBEDDED SYSTEMS
Program:
/*
* linkdlst.c
*
* Created on: Oct 5, 2022
* Author: Admin
*/
#include
#include
//defining a node using structure
struct node
{
int value;
struct node *next;
};
struct node *head_1 = NULL;
struct node *head_2 = NULL;
struct node *union12 = NULL;
struct node *intersec12 = NULL;
//function for inserting a node at the beginning
void InsertNode(struct node **head, int num)
{
//creating a new node
struct node *new;
//allocating memory
new = malloc(sizeof(struct node));
//initializing data part of the node
new -> value = num;
//link part points to head node of linked list
new -> next = *head;
//assign new node as head node
*head = new;
}
//function for displaying the linked lists
void Display(struct node *p)
{
while (p != NULL)
{
printf("%d \n", p -> value);
p = p -> next;
}
}
//function to compare the node data with a value by linearly traversing across head to tail in a linked list
int compare(struct node *head, int num)
{
while (head != NULL)
{
if (head -> value == num)
return 1;
head = head -> next;
}
return 0;
}
//function to get the union of two linked lists
struct node *get_union(struct node *head_1, struct node *head_2)
{
union12 = NULL;
struct node *p1 = head_1;
//add all the nodes from linked list 1 to union12
while (p1 != NULL)
{
InsertNode(&union12, p1 -> value);
p1 = p1 -> next;
}
//add those values which are not present in union12 by comparing Linked list 1 and 2
while (head_2 != NULL)
{
if (!compare(head_1, head_2 -> value))
{
InsertNode(&union12, head_2 -> value);
}
head_2 = head_2 -> next;
}
return union12;
}
//function to get the intersection of two linked lists
struct node *get_intersection(struct node *head_1, struct node *head_2)
{
intersec12 = NULL;
//add those values present in both linked lists by comparing the values of linked list 1 and 2
while (head_1 != NULL)
{
if (compare(head_2, head_1 -> value))
{
InsertNode(&intersec12, head_1 -> value);
}
head_1 = head_1 -> next;
}
return intersec12;
}
int main()
{
int i, n1, n2, temp;
//creating first linked list
printf("Enter the number of nodes in first linked list: ");
fflush(stdout);
scanf("%d", &n1);
printf("Enter the values for %d nodes: ", n1);
fflush(stdout);
for (i = 0;i < n1; i++)
{
scanf("%d", &temp);
InsertNode(&head_1, temp);
}
Display(head_1);
//creating second linked list
printf("Enter the number of nodes in second linked list: ");
fflush(stdout);
scanf("%d", &n2);
printf("Enter the values for %d nodes: ", n2);
fflush(stdout);
for (i = 0;i < n2; i++)
{
scanf("%d", &temp);
InsertNode(&head_2, temp);
}
Display(head_2);
//creating union of both linked lists
get_union(head_1, head_2);
printf("Union of the two linked lists: \n");
Display(union12);
//creating intersection of both linked lists
get_intersection(head_1, head_2);
printf("Intersection of the two linked lists: \n");
Display(intersec12);
}
Output:
