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 - 12 & 14
AIM: To create a user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List.
Overview of the Project: (5 to 6 lines)
- In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists
Goals (2 to 3 points)
- Implementation of interface for mathematical Set Theory using C programming.
- Giving two list of data as input to get the output as union & intersection for given list of data.
Key Highlights of the Project: (4 to 5 point)
- Creating two list of data using linked list method.
- Insert the node to 1st and 2nd list accordingly.
- Intersection of data of two lists.
- Getting Union of two list.
Deliverables of this project: (4 to 5 points)
- Intersection of data of two list.
- Union of data of two list
Expected Outcomes: (4 to 5 points)
- Student will get the Knowledge of creating the nodes using Linked List.
- Getting the intersection and Union of data of two list.
- Comparing the elements of two list.
The code for the whole project is as follows:
#include
#include
//node is defined here using the structure as the node contains two parts
struct node
{
int data;
struct node *next;
};
//various structure pointer variables are declare as null in the below few lines
struct node *head_1 = NULL;
struct node *head_2 = NULL;
struct node *union1 = NULL;
struct node *intersec1 = NULL;
//function used for inserting a node at the beginning of the list
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 -> data = 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 -> data);
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 -> data == 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)
{
union1 = NULL;
struct node *p1 = head_1;
//add all the nodes from linked list 1 to union1
while (p1 != NULL)
{
InsertNode(&union1, p1 -> data);
p1 = p1 -> next;
}
//add those values which are not present in union1 by comparing Linked list 1 and 2
while (head_2 != NULL)
{
if (!compare(head_1, head_2 -> data))
{
InsertNode(&union1, head_2 -> data);
}
head_2= head_2 -> next;
}
return union1;
}
//function to get the intersection of two linked lists
struct node *get_intersection(struct node *head_1, struct node *head_2)
{
intersec1 = 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 -> data))
{
InsertNode(&intersec1, head_1 -> data);
}
head_1= head_1 -> next;
}
return intersec1;
}
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(union1);
//creating intersection of both linked lists
get_intersection(head_1, head_2);
printf("Intersection of the two linked lists: \n");
Display(intersec1);
}