Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
I gave the below inputs
First List : 5 3 8 7
Second List : 7 9 5 1
Union Output : "Here is the result of union: 9 1 5 3 8 7 "
Intersection Output : "Here is the result of intersection: 5 7 "
(Kindly Refer Last two screenshots)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <stdbool.h>
typedef struct node //defing the structure
{
int data; //creating the data type of the variable
struct node *next; //creating a self referential node
}
Node;//changing the structure name as Node
static Node *head1, *head2, *head, *head_union, *head_intersection, *newnode, *tail, *res_union, *res_intersection;
//declaring different functions with arguments
Node *insert_node(Node *head_ref, int n);
void display_list(Node *head_ref);
Node *get_union(Node *head_pointer1, Node *head_pointer2);
Node *get_intersection(Node *head_pointer1,Node *head_pointer2);
int compare(Node *head,int data);
void display_list(Node *head_ref) //function used to display the nodes data
{
if(head_ref == 0){
printf("\nThis List is empty\n");
}else{
tail = head_ref; //we are transferring address if the list to tail
while(tail != 0){
printf("%d ", tail->data);//prints the data of the current node
tail = tail->next;//advances the position of current node
}
tail = newnode;
}
}
Node *insert_node(Node *head_ref,int n){ //function used to create the new list
printf("\nInserting the Elements at the beginning..\n");
newnode = (Node*) malloc(sizeof(Node));//allocating the memory to the nodes
printf("Address of created newnode : %d\n",newnode);
newnode->data = n;
printf("Inserted value is : %d\n",newnode->data);
newnode->next = head_ref;//address of the previous address is transferred to the next node
head_ref = newnode;
return head_ref;
}
Node *get_union(Node *head_pointer1,Node *head_pointer2)//function used to do union of the list
{
while(head_pointer1 != 0){
head_union = insert_node(head_union,head_pointer1->data);//inserting the element
head_pointer1 = head_pointer1->next;//travelling to next node
}
while(head_pointer2 != 0){
if(!compare(head_union,head_pointer2->data))//here we are inserting the element from list
head_union = insert_node(head_union,head_pointer2->data);
head_pointer2 = head_pointer2->next;//travelling to next node
}
return head_union;
}
int compare(Node *head,int data){
while(head != NULL){
if(head->data == data) //comparing the data of the list2 with list head_pointer1
return 1;
head = head->next;
}
return 0;
}
Node *get_intersection(Node *head_pointer1,Node *head_pointer2)
{
while(head_pointer1 !=0){
if(compare(head_pointer2, head_pointer1->data))//we are comparing both the list
head_intersection = insert_node(head_intersection,head_pointer1->data);
head_pointer1 = head_pointer1->next;
}
return head_intersection;
}
int main()
{
int first_list_nodes = 0, second_list_nodes = 0,first_list_items = 0,second_list_items = 0;
int f;
printf("Enter the number of Nodes in first List:");
fflush(stdout);
scanf("%d",&first_list_nodes);
printf("Enter the %d elements:",first_list_nodes);
for(f=0;f<first_list_nodes;f++){
fflush(stdout);
scanf("%d",&first_list_items); //getting the data in run time
head1 = insert_node(head1,first_list_items);//inserting the data in list
}
display_list(head1);//displaying the head list
printf("\nEnter the number of Nodes in second List:");
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("Enter the %d elements:",second_list_nodes);
for(f=0;f<second_list_nodes;f++){
fflush(stdout);
scanf("%d",&second_list_items);//getting the data in run time
head2 = insert_node(head2,second_list_items);
}
display_list(head2);//displaying the head1 List
printf("\n*****Union & Intersection Results********\n");
res_union = get_union(head1, head2);//transferring the result of union function
printf("\nHere is the result of union: ");
display_list(res_union);//transferring the values as arguments to display function
res_intersection = get_intersection(head1,head2);//transferring the result of intersection
printf("Here is the result of intersection: ");//printing the output
display_list(res_intersection);//transferring the values as arguments to display function
return 0;
}