Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/* here these project is mathematical operation. here told a many operation working like union & intersection list create as a linked and lisr. here a create a collection this create many data. here using the refrence address, the main concept of linkedist is self refferal structure.*/
Implementation Of C interface for mathematical set theory using C progamming:
first create a structure as a user define name here a create two DATA TYPES int for staring the integer value & the pointer variable value using storing the address of the same structure itself.
then after a creating a main function of list 1 & list 2 the loop & using then many fucntion that's type diffrent function creating in main program .
Creating two lists of data using the linked list method:
first we are getiing a size of list & here using a for loop are inserting a data inside the for loop we use a function node. these function we are add a value & address from the user, inside the inserted node we are creating a size of list using a malloc function,
entering the data in data area and storing the address of the head to the next pointer & we are the storing a new node address in the head of next updates.
Code is Mention no 1.
Getting Union of two lists:
here a fucntion of union with the arguments head1 & head2.
the union function we are using a head unit as a list by inverting to the value of the first list the union & union jumping to next area by address store in next, insert a node fucntion thats time is insert used a creation of the linked lisr.
Getting Intersection of two lists:
the intersection to easy to get & just compare both value & return value of which is list both are commanly. these command useally use for compare both command list1&list2
that time a condtion is passed then next step is inserting the data into list.
Display function:
here the data pack diffrent list vs diffrent arguments paasing to display funtion, then we are creating a sample null head & print data & use a next address store in the pointer variable pointing the structure.
lets see the code of this project
CODE:
#include <stdio.h>
#include <stdlib.h>
typedef struct node //defining the structure
{int data;
struct node *next;}
node;// changing the structure name as node
static Node
*head1,*head2,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersecton;
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,Nide *head_pointer2);
int compare(Node *head, int data);
void display_list(Node *head_ref)
{if(head_ref == 0)
{printf(" List is empty. ");}
else{tail = head_ref;
while(tail != 0)
{ Printf("%d", tail->data);
tail = tail->next;}
tail = newnode;}
Node *insert_node(Node *head_ref, int n)
{printf("nInserting Elements at the beginning...n");
newnode =(Node*) malloc(size of (Node));
printf("Address of created newnide :%dn",newnode);
newnode->data = n;//assigning the value to node
printf("Inserted value is :%dn", newnode->data);
newnode->next = head_ref;//address of the previous address s transfered to the newnode
head_ref = newnode;//transfering the new address & new node
return head_ref;
}
Node *get_union(Node *head_pointer1,Node *head_pointer2)
{while(head_pointer1 !=0)
{head_union = insert_node(head_union,head_pointter1->data);
head_pointer1 = head_pointer1->next;}
while(head_ponter2 != 0)
{If( !compare(head_union,head_pointer2->data)) //union of data list
head_union = insert_node(head_union,head_pointer2->data); //inserting the data
head_pointer2 = head_pointer2->next; //ttravel to next node}
return head_union;}
int compare(Node *head,int data)
{while (head != NULL)
{if (head->data ==data) //comparing the data of list 2 with list 1
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))
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 %d the elements:", first_list_nodes);
for(f=0; f<first_list_nodes; f++)<="" p=""></first_list_nodes;>
{fflush(stdout);
scanf("%d",&first_list_items);
head1 = insert_node(head1,first_list_items);
display_list(head1);
printf("nEnter the number of Nodes in second List:");
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("Enter %d the elements:", second_list_nodes);
for(f=0; f<seconf_list_nodes; f++)<="" p=""></seconf_list_nodes;>
{fflush(stdout);
scanf("%d",&second_list_items);//getting the data in the run time
head2 = insert_node(head2, second_list_items);}
display_list(head2);
res_union = get_union(head1 , head2);
printf("Here is the result of union:");
display_list(res_union);
res_intersection = get_intersection(head1, head2);
printf("Here is the result of intersection:");
display_list(res_intersection);
}