Challenge

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

two linked lists created and then created functions insert Node, Get Union, Get intersection, compare and display to perform the union and intersection to the list, Few popular operations can be performed on these sets which are intersection(list of all common elements/members of two or more sets)

Set theory is a branch of mathematics that deals with the properties of a well-defined collection of objects, which may or may not be of a mathematical nature, such as numbers or functions. Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous. so here we are creating a structure it has a collection of different data. Here is the process to first create two lists

A user-friendly interface for the user to enter elements/members of sets. Computation of the Intersection of the provided two sets by the user. The new node is always added before the head of the given linked list. And newly added node becomes the new head of the linked list. the elements are linked using pointers. They include a series of connected nodes. Here, each node stores the data and the address of the next node.

Entering the data in the data area, and storing the address of the head to the next pointer and we are storing the new node address in the head for the next update

#include"list.h" //header//
int main() //main function//
{
int first_list_nodes=0,second_list_nodes=0,first_list_items=0, //local variable// //here given nodes,items//
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); // here declaring the first_list_items//
}
display_list(head1); //displaying list of header1//
printf("\nEnter the number of nodes in second list:"); //printing the node2 list of the console//
fflush(stdout);
scanf("%d",&second_list_nodes); //reads the address//
printf("\nEnter %d the elements:",second_list_nodes); //printing the list2 in the console//
for(int i=0;i < second_list_nodes;i++) //for loop will starts second_list_nodes here//
{
fflush(stdout);
scanf("%d",&second_list_items); //here declaring the second_list_items//
head2=insert_node(head2,second_list_items); //here declaring the second_list_items//
}
display_list(head2); //displaying the list of head1 and head2//
res_union = get_union(head1,head2); //result of the hard1 and head2 will store the memory of the same location//
#ifdef DEBUG_MAIN
printf("\nValue of Head1 and Head2 :%d %d\n",head1,head2); //debugging the console of the head1 and head2//
#endif
#ifdef DEBUG_MAIN
printf("\nvalue of Head1 and Head2 after performing union :%d %d\n",head1,head2);
printf("\nValue of res_union :%d\n",res_union);
#endif
printf("Here is the result of union:"); //end of the result will be display in the console//
display_list(res_union);
res_intersection=get_intersection(head1,head2); //result of the get intersection of head1 and head2//
#ifdef DEBUG_MAIN
printf("\nValue of Head1 and head2 after performing union :%d %d\n",head1,head2);
printf("\nValue of res_intersection :%d\n",res_intersection);
#endif
printf("\nHere is the result of intersection:"); //enfif is the final result will be display in the console//
display_list(res_intersection);
}
list.c
#include"list.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) //insert the number of nodes//
{
printf("\nInserting Element at the beginning...\n"); //inserting the number of the elements in the console//
newnode=(Node*)malloc(sizeof(Node)); //here giving the size of the malloc node//
printf("Address of created newnode :%d\n",newnode); //printf is the printing address of the newnode//
newnode->data=n; //number of the newnode data//
printf("Inserted value is :%d\n",newnode->data); // inserted value of newnode data will be display on the console// newnode->next =head_ref; //here performing the next means loop will be created of the newnode data values//
head_ref=newnode; //reference of the newnode data is equal to head//
return head_ref; //return reference//
}
Node *get_union(Node *head_pointer1,Node *head_pointer2)//here node of pointer1 and pointer2 can be shared by the same memory location//
{
while(head_pointer1 !=0) //while loop starts head_pointer1 is not equal to the 0//
{
head_union= insert_node(head_union,head_pointer1->data); //inserting the data of the pointer1//
head_pointer1=head_pointer1->next; //its will go to the next of the value of new pointer next//
}
while(head_pointer2 !=0) //while loop starts head_pointer2 is not equal to the 0//
{
if(! compare(head_union,head_pointer2->data)) //here comparing the pointer1 and pointer2 of the data//
head_union=insert_node(head_union,head_pointer2->data); //inserting the data of the pointer2//
head_pointer2=head_pointer2->next; //its will go and take next of the value of new pointer2 next//
}
return head_union; //return union//
}
int compare(Node *head,int data) //compare the both *head data//
{
while(head != NULL) //while loop will start,here head is not equal to 0 is the condition//
{
if(head->data == data) //here head data is equal to data then shows the return 1//
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)//A set that consists of elements common will present in pointer1 and pointer2//
{
while (head_pointer1 !=0) //while loop will starts here head_ pointer is not equal to 0//
{
if(compare(head_pointer2,head_pointer1->data)) //here comparing both elements are same or not//
head_intersection = insert_node(head_intersection,head_pointer1->data); //inserting the data of the pointer1//
head_pointer1 = head_pointer1->next; //its will go and take next of the value of new pointer1 next//
}
return head_intersection;
}
truct node* intersection(struct node *FirstLL, struct node *SecondLL)
{
IntersectLL = NULL;
//Search every element of FirstLL in SecondLL, If found then add it to intersection List
while(FirstLL != NULL)
{
if(search(SecondLL, FirstLL->data))
{
insert(&IntersectLL, FirstLL->data);
}
FirstLL = FirstLL->next;
}
return IntersectLL;
}

{
if(!search(LLOne, LLTwo->data))

{
insert(&unionLL, LLTwo->data);
}
LLTwo = LLTwo->next;
}
return unionLL;
}


//Returns the Linked List which contains common nodes of two given linked list

struct node* intersection(struct node *LLOne, struct node *LLTwo)

{
intersectionLL = NULL;
//Search every element of LLOne in LLTwo, If found then add it to intersection List
while(LLOne != NULL)

{
if(search(LLTwo, LLOne->data))

{
insert(&intersectionLL, LLOne->data);
}
LLOne = LLOne->next;
}
return intersectionLL;
}
//Prints a linked list from head node till tail node

void printLinkedList(struct node *nodePtr)

{
while (nodePtr != NULL)

{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");

}
}

int main()

{
int i, LLOneCount, LLTwoCount, temp;
initialize();
//Creating First linked List
printf("Enter number of nodes in first Linked List\n");
fflush(stdout);
scanf("%d", &LLOneCount);
printf("Enter %d integers\n", LLOneCount);
for(i=0; i<LLOneCount; i++)

{
scanf("%d", &temp);
insert(&LLOne, temp);
}
printLinkedList(LLOne);
printf("\nEnter number of nodes in second Linked List\n");
fflush(stdout);
scanf("%d", &LLTwoCount);
printf("Enter %d integers\n", LLTwoCount);
for(i=0; i<LLTwoCount; i++)

{
scanf("%d", &temp);
insert(&LLTwo, temp);
}
printLinkedList(LLTwo);
//printing Union of two given linked list
findunion(LLOne, LLTwo);
intersection(LLOne, LLTwo);
printf("\nUnion Linked List\n");
printLinkedList(unionLL);
printf("\nIntersection Linked List\n");
printLinkedList(intersectionLL);

return 0;
}

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.