Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Overview of the Project:
The project is representating some of the operation related to set theory i.e., union and intersection of the 2 sets.
This is an user interactive program, user can give how many no of element should be present in each set and their values using the console.
The program has been implemented using the concept of linked list and function pointer.
Goals:
- To find the union and intersection of 2 sets as of set theory.
- To take the input from the user i.e., the data of the 2 sets and then finding out the union and intersection of 2 sets.
- To use the concept of linked list and self referencial structure of the c Programming.
Key Highlights
- The program is implemented using the concept of Linked list.
- User give the input like no of element in each set to create the 2 sets.
- User give the input to insert the data element to the set.
- The program will find the union of the 2 given sets.
- Also find out the intersection of the 2 given sets.
Deliverables of this project:
- This is an user interactive program where user decide the no of element in the set and thier value.
- 2 different set of values are taken for the set theory operation
- Union of the 2 set of data is the output.
- Intersection of data of 2 sets.
Expected Outcomes:
- Union and intersection of data of 2 list.
- Hands-on on the concept of linked list, Self referential structure, function pointers.
- User interactive program.
- understanding about Structure.
CODE:
#include<stdio.h>
#include<stdlib.h>
// creating a structure, representing the node
struct node
{
int value; // variable to store the data of the node
struct node * next; // self refertial pointer to store the adress of the next node
};
// Declaring the structure obeject
struct node * head_1;
struct node * head_2;
struct node * unin;
struct node * intrscn;
//function declaration
struct node * insert(struct node*, int);
void display(struct node *);
struct node* list_union(struct node *, struct node *);
struct node * intersection(struct node *, struct node *);
int compare(struct node* head, int x);
// function for insert the node
struct node * insert(struct node * head_ref, int n)
{
struct node * new_node = (struct node *)malloc(sizeof(struct node));
new_node -> value = n;
new_node -> next = head_ref;
head_ref = new_node;
}
// To display the linked list
void display(struct node * head)
{
if(head == NULL)
{
printf("No List is Present");
}
else
{
while(head != NULL)
{
printf("%d ",head->value);
head = head -> next;
}
}
}
// function to find out the union of 2 list
struct node* list_union(struct node * href1, struct node * href2)
{
struct node * lst_un;
while(href1 != NULL)
{
lst_un = insert(lst_un, href1 -> value);
href1 = href1->next;
}
while(href2 != NULL)
{
if(!compare(lst_un,href2->value))
{
lst_un = insert(lst_un, href2 -> value);
}
href2 = href2->next;
}
return lst_un;
}
// function to find out the intersection of 2 list
struct node * intersection(struct node * head1, struct node * head2)
{
struct node * res = NULL;
struct node * temp =head1;
while(temp != NULL)
{
if(compare(head2, temp->value))
{
res = insert(res,temp->value);
}
temp = temp->next;
}
return res;
}
// function to compare the 2 list
int compare(struct node* head, int x)
{
while(head != NULL )
{
if(head ->value == x)
{
return 1;
}
head = head->next;
}
return 0;
}
// main function controll the entire flow of the function
int main()
{
int node_data, node_data_2;
int list1_count, list2_count;
printf("Enter the No of node in list 1:");
scanf("%d",&list1_count);// getting the no of node in the list 1
printf("Enter the Element for list 1");
// getting the list of the element
for(int i = 1;i <= list1_count;i++)
{
scanf("%d",&node_data);
head_1 = insert(head_1,node_data);
}
printf("\nThe Element of the List 1 is:");
display(head_1); // displaying the list 1.
printf("\nEnter the No of node in list 2:");
scanf("%d",&list2_count); // getting the no of node in the list
printf("\nEnter the Element for list 2");
for(int i = 1;i <= list2_count;i++)
{
scanf("%d",&node_data_2);
head_2 = insert(head_2,node_data_2);
}
printf("\nThe Element of the List 2 is:");
display(head_2); // displaying the list 2.
printf("\nThe union of 2 list:");
unin = list_union(head_1,head_2); // getting the union of the 2 list
display(unin); // displaying the union list.
printf("\nThe intersection of 2 list:");
intrscn = intersection(head_1, head_2);// getting the intersection of the 2 list
display(intrscn); // displaying the intersection list.
return 0;
}