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.
Code:
In main.c :
#include"operation.h"
int main()
{
int first_list_nodes=0,second_list_nodes=0,first_list_items=0, //local variable//nodes and items//
second_list_items=0;
printf("\n Enter the number of nodes in first list:");//printing the node1 list of the console
fflush(stdout);
scanf("%d",&first_list_nodes); // reads the address
//printing list 1 in console
int i;
for( i=0;i < first_list_nodes;i++) //for loop
{
printf("\n Enter the %d the element: ",first_list_nodes);
fflush(stdout);
scanf("%d",&first_list_items); //reads the address
head1=insert_node(head1,first_list_items); //here declaring first list items
}
display_list(head1); // displaying list of header1
printf("\n Enter the number of nodes in second list:"); //printing node2 list of console
fflush(stdout);
scanf("%d",&second_list_nodes); //printing list 2 in console
for( i=0; i <second_list_nodes;i++) //for loop starts
{
printf("Enter the elements in second list");
fflush(stdout);
scanf("%d",&second_list_items); //reads the address
head2=insert_node(head2,second_list_items); //declaring second list items
}
display_list(head2); //displaying list of head1 and head2
res_union=get_union(head1,head2);//result of head 1 and head2 will store the memory of same location
#ifdef DEBUG_MAIN
printf("\n value of head1 and head2:%d %d\n",head1,head2); //debugging the console of head1 and head2
#endif
#ifdef DEBUG_MAIN
printf("\n value if head1 and head2 after performing union:%d %d\n",head1,head2);
printf("\n value of res_union:%d\n",res_union);
#endif
printf("\n Here is result of union:"); //final result will display in console
display_list(res_union);
res_intersection=get_intersection(head1,head2); //result of get intersection of head 1 and head2
#ifdef DEBUG_MAIN
printf("\n value of head1 and head2 after performing union:%d %d\n",head1,head2);
printf("\n value of res_intersection:%d\n",res_intersection);
#endif
printf("\n Here is the result of intersection:"); //endif is the final result will be display in console
display_list(res_intersection);
}
In. Operation.c:
#include"operation.h" // header
void display_list(Node *head_ref) //displaying the list of referance in the head
{
if(head_ref==0) // here the condition is equal to zero
{
printf("list is empty"); // above condition will satisfy then print will be display
}
else
{
tail=head_ref;
while(tail!=0) // while loop will starts tail is not equal to 0
{
printf("%d",tail->data); // if condition is not satisfied then the else part will be display
tail=tail->next;
}
tail=newnode;
}
}
Node *insert_node(Node *head_ref,int n) // insert the number of node
{
printf("\n inserting Element at the begining....\n"); // insert the number of elements in the console
newnode=(Node*)malloc(sizeof(Node)); // printing
printf("Address of created new node:%d\n",newnode);
newnode->data=n; // number of new node data
printf("inserted value is :%d\n",newnode->data); // inserted value of new node data will be display on the console
newnode->next=head_ref;
head_ref=newnode;
return head_ref; // return reference
}
Node *get_union(Node *head_pointer1,Node *head_pointer2) // node of head pointer 1 and 2 can be shared by same memory location
{
while(head_pointer1!=0) // while loop starts head_pointer1 is not equal to 0
{
head_union=insert_node(head_union,head_pointer1->data); // inserting the data of pointer1
head_pointer1=head_pointer1->next;
}
while (head_pointer2!=0)
{
if(!compare(head_union,head_pointer2->data)) // comparing the pointer1 and pointer2
head_union=insert_node(head_union,head_pointer2->data); // inserting data of pointer2
head_pointer2=head_pointer2->next;
}
return head_union; // return union
}
int compare(Node *head,int data) // compare both head data
{
while(head!=NULL)
{
if(head->data==data)
return 1;
head=head->next; //here head is equal to next data then it will return zero
}
return 0;
}
Node *get_intersection(Node *head_pointer1,Node *head_pointer2)
{
while(head_pointer1!=0)
{
if(compare(head_pointer2,head_pointer1->data)) //here comparing both the element are same or not
head_intersection=insert_node(head_intersection,head_pointer1->data);
head_pointer1=head_pointer1->next;
}
return head_intersection; //executed successfully
}
in Operation.h file:
#ifndef OPERATION_H_ //preprocessor
#define OPERATION_H_ //preprocessor
#include<stdio.h> //preprocessor
#include<stdlib.h> //preprocessor
#include<stdbool.h> //preprocessor
typedef struct node //a new name for same existing data type
{
int data; // local variable
struct node *next; // declaration of struct
}Node;
static Node *head1,*head2,*head_union,*head_intersection,*newnode,*tail,*res_intersection,*res_union,*head_ref; // call the function
Node *insert_node(Node *head_ref,int n);// call the function of insert node
void display_list(Node *head_ref);//display the referance
Node *get_union(Node *head_pointer1,Node *head_pointer2);// call the all memory location
Node *get_intersection(Node *head_pointer1,Node *head_pointer2); //call the set of variable
int compare(Node *head,int data); // call the both node data
#endif /* OPERATION_H_ */
Output:
Enter the number of nodes in first list:4
Enter the 4 the element: 1 2 3 4
inserting Element at the begining....
Address of created new node:9839584
inserted value is :1
Enter the 4 the element:
inserting Element at the begining....
Address of created new node:9839616
inserted value is :2
Enter the 4 the element:
inserting Element at the begining....
Address of created new node:9859040
inserted value is :3
Enter the 4 the element:
inserting Element at the begining....
Address of created new node:9859072
inserted value is :4
4321
Enter the number of nodes in second list:4
Enter the elements in second list 2 3 4 5
inserting Element at the begining....
Address of created new node:9859104
inserted value is :2
Enter the elements in second list
inserting Element at the begining....
Address of created new node:9859136
inserted value is :3
Enter the elements in second list
inserting Element at the begining....
Address of created new node:9859168
inserted value is :4
Enter the elements in second list
inserting Element at the begining....
Address of created new node:9859200
inserted value is :5
5432
inserting Element at the begining....
Address of created new node:9859232
inserted value is :4
inserting Element at the begining....
Address of created new node:9859264
inserted value is :3
inserting Element at the begining....
Address of created new node:9859296
inserted value is :2
inserting Element at the begining....
Address of created new node:9859328
inserted value is :1
inserting Element at the begining....
Address of created new node:9859360
inserted value is :5
Here is result of union:51234
inserting Element at the begining....
Address of created new node:9859392
inserted value is :4
inserting Element at the begining....
Address of created new node:9859424
inserted value is :3
inserting Element at the begining....
Address of created new node:9859888
inserted value is :2
Here is the result of intersection:234
Output: In operation.h

In operation.c



in main.c

