Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

abhinaya mam..

// project 1

// Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using // Linked List.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct node // defining the structure
{
int data; //creating the data type of the variable
struct node *next; //creating 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 function 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("list is empty.");
}
else
{
tail = head_ref; //we are transferring address if the list to tail
while(tail != 0)
{
printf("%d", tail->data); //prints the data of current node
tail = tail->next; //advances the position of current node
}
tail = newnode;
}
}

node *insert_node(node *head_ref,int n) // function used create the new list
{
printf("\ninserting 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; //assigning the value to node
printf("inserted value is :%d\n",newnode->data);
newnode->next = head_ref; //address of the pervious address is transfered to the new node
head_ref = newnode; //transferring the new address to the head ref for linking new node
return head_ref;
}

node *get_union(node *head_pointer1,node *head_pointer2) //function used to union to list
{
while(head_pointer1 !=0)//1 2 3 4
{
head_union = insert_node(head_union,head_pointer1->data); //here we are inserting the data of list 1 into the union function
head_pointer1 = head_pointer1->next; //we transverse the node using the address in next

}
while(head_pointer2 != 0) // 2 3 4 5
{
if(!compare(head_union,head_pointer2->data))//here we are inserting the element from list 2 number where not in the list 1
head_union = insert_node(head_union,head_pointer2->data); //inserting the element not in the list 2
head_pointer2 = head_pointer2->next; // travelling to next node
}
return head_union; // 1 2 3 4 5
}

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)) //we are comparing the both list with intersection
head_intersection = insert_node(head_intersection,head_pointer1->data);//transferring that value to the intersection
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); // 4(1 2 3 4)
printf("enter %d the elements:",first_list_nodes); //getting the number of element in the lists
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 head1 list
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 < 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
res_union = get_union(head1,head2); // transferring the result of union function to the res union
printf("here 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 func to the res intersection

printf("here is the result of intersection: ");
display_list(res_intersection);//transferring the values as arguments to display function

}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.