Challenge

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

Linked.h file

#include"linked.h" //header file//
int main() //main function//
{

int first_list_nodes=0,second_list_nodes=0,first_list_items=0, //int declaration//
second_list_items=0;
printf("\nEnter the number of nodes in first list:"); //printing the node1 list of the console//
fflush(stdout);
scanf("%d",&first_list_nodes); //reads the address//
printf("\nEnter %d the elements:",first_list_nodes); //printing the list1 in the console//
for(int i=0;i < first_list_nodes;i++) //for loop will starts first_list_nodes here//
{
fflush(stdout);
scanf("%d",&first_list_items); //reads the address//
head1=insert_node(head1,first_list_items); // insert operation is done and is stored in head1//
}
display_list(head1); //displaying list of head1//
printf("\nEnter the number of nodes in second list:"); //repeating the same procedure for second_list//
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("\nEnter %d the elements:",second_list_nodes);
for(int i=0;i < second_list_nodes;i++)
{
fflush(stdout);
scanf("%d",&second_list_items);
head2=insert_node(head2,second_list_items);
}
display_list(head2); //displaying the list of head2//

res_union = get_union(head1,head2); //result of union operation is performed for head1 and head2//
printf(" the result of union:"); //printing the result of union//
display_list(res_union); // display the res_union//

res_intersection=get_intersection(head1,head2); //result of intersection operation is performed for head1 and head2//
printf("\nHere is the result of intersection:"); //printing the result of intersection//
display_list(res_intersection); // display the res_intersection//

}

Main.c file

#include"linked.h" //header file//
int main() //main function//
{

int first_list_nodes=0,second_list_nodes=0,first_list_items=0, //int declaration//
second_list_items=0;
printf("\nEnter the number of nodes in first list:"); //printing the node1 list of the console//
fflush(stdout);
scanf("%d",&first_list_nodes); //reads the address//
printf("\nEnter %d the elements:",first_list_nodes); //printing the list1 in the console//
for(int i=0;i < first_list_nodes;i++) //for loop will starts first_list_nodes here//
{
fflush(stdout);
scanf("%d",&first_list_items); //reads the address//
head1=insert_node(head1,first_list_items); // insert operation is done and is stored in head1//
}
display_list(head1); //displaying list of head1//
printf("\nEnter the number of nodes in second list:"); //repeating the same procedure for second_list//
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("\nEnter %d the elements:",second_list_nodes);
for(int i=0;i < second_list_nodes;i++)
{
fflush(stdout);
scanf("%d",&second_list_items);
head2=insert_node(head2,second_list_items);
}
display_list(head2); //displaying the list of head2//

res_union = get_union(head1,head2); //result of union operation is performed for head1 and head2//
printf(" the result of union:"); //printing the result of union//
display_list(res_union); // display the res_union//

res_intersection=get_intersection(head1,head2); //result of intersection operation is performed for head1 and head2//
printf("\nHere is the result of intersection:"); //printing the result of intersection//
display_list(res_intersection); // display the res_intersection//

}

Linked.c

#include"linked.h" //header//
void display_list(Node *head_ref) //displaying the list of the reference in the head//
{
if(head_ref==0) //here the condition is head_ref is equal to the 0//
{
printf(" list is empty"); //above the condition will be satisfied then the printf will be display//
}
else
{
tail=head_ref;
while(tail !=0) //while loop starts tail is not equal to the 0//
{
printf("%d", tail->data); //if condition is not satisfied then the else part will be display.after its will next list//

tail=tail->next;
}
tail=newnode;
}
}
Node *insert_node(Node *head_ref,int n) //inserting the node using the function//
{
printf("\nInserting Element at the beginning...\n"); //inserting the number of the elements in the console//
newnode=(Node*)malloc(sizeof(Node)); //newnode holds the address this lines mainly to allocate the memory//
printf("Address of the newnode :%d\n",newnode); // printing address of the newnode//
newnode->data=n; //n holds the value//
printf("Inserted value is :%d\n",newnode->data); // inserted value of newnode data //
newnode->next=head_ref; // newnode->next=head_ref(address of node 2)//
head_ref=newnode; //newnode is move to head_ref/
return head_ref; //return head_ref//
}
Node *get_union(Node *linked_list1,Node *linked_list2) //union between linked_list1 and linked_list2//
{
while(linked_list1 !=0) //while loop starts linked_list1 is not equal to the 0//
{
head_union= insert_node(head_union,linked_list1->data); //inserting the data of the linked_list1//
linked_list1=linked_list1->next; //its will go to the next of the value of new linked_list next//
}
while(linked_list2 !=0) //while loop starts linked_list2 is not equal to the 0//
{
if(! compare(head_union,linked_list2->data)) //here comparing the linked_list1 and linked_list2 of the data//
head_union=insert_node(head_union,linked_list2->data); //inserting the data of the linked_list2//
linked_list2=linked_list2->next; //its will go and take next of the value of new linked_list2 next//
}
return head_union; //return union//
}
int compare(Node *head,int data) //head is holding both head1 and head2 and data also compare both//
{
while(head != NULL) //while loop will start,here head is not equal to NULL is the condition//
{
if(head->data == data) //if head and data are same then it will return 1//
return 1;
head = head->next; //if head and data are not same then it will go next//
}
return 0;
}
Node *get_intersection(Node *linked_list1,Node*linked_list2) //select the common element present in linked_list1 and linked_list2//
{
while (linked_list1 !=0) //while loop will starts here linked_list1 is not equal to 0//
{
if(compare(linked_list2,linked_list1->data)) //keeping linked_list2 constant and then comparing it with linked_list1//
head_intersection = insert_node(head_intersection,linked_list1->data); //inserting the data of the linked_list1//
linked_list1 = linked_list1->next; //its will go and take next of the value of new of linked_list1 //
}
return head_intersection;
}

output:

Enter the number of nodes in first list:4

Enter 4 the elements:1 2 3 4

Inserting Element at the beginning...
Address of the newnode :7740472
Inserted value is :1

Inserting Element at the beginning...
Address of the newnode :7740488
Inserted value is :2

Inserting Element at the beginning...
Address of the newnode :7740504
Inserted value is :3

Inserting Element at the beginning...
Address of the newnode :7740520
Inserted value is :4
4321
Enter the number of nodes in second list:4

Enter 4 the elements:2 3 4 5

Inserting Element at the beginning...
Address of the newnode :7740536
Inserted value is :2

Inserting Element at the beginning...
Address of the newnode :7740552
Inserted value is :3

Inserting Element at the beginning...
Address of the newnode :7740664
Inserted value is :4

Inserting Element at the beginning...
Address of the newnode :7740680
Inserted value is :5
5432
Inserting Element at the beginning...
Address of the newnode :7740696
Inserted value is :4

Inserting Element at the beginning...
Address of the newnode :7740712
Inserted value is :3

Inserting Element at the beginning...
Address of the newnode :7740728
Inserted value is :2

Inserting Element at the beginning...
Address of the newnode :7740744
Inserted value is :1

Inserting Element at the beginning...
Address of the newnode :7740760
Inserted value is :5
the result of union:51234
Inserting Element at the beginning...
Address of the newnode :7740776
Inserted value is :4

Inserting Element at the beginning...
Address of the newnode :7740792
Inserted value is :3

Inserting Element at the beginning...
Address of the newnode :7740808
Inserted value is :2

Here is the result of intersection:234

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.