Challenge

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

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

Overview of the Project:

  1. This is the program of an mathematical operation of union and intersection.
  2. In this program I have used the linked list for the set values. There are different types of linked lists are there in C. But perticularly in this program I have used the single link list program.
  3. In this i have used the strucres for simplyfying the program.
  4. For the function of union I have used the compare operation.
  5. In the result of this program I have printed the input values,output linked list values, address of the linked list values.
  6. For the proper implementation I have used structure functions as a barior.

Goals :

  1. The main goal of this program is to print the values of union and intersection with linked list variable.
  2. So what we need to do is to load the values and then implement the operation for the linked list and then we can perform the operation of union and intersection.

Key Highlights of the Project:

  • Firest we need to declare the variables what do we use in this program.
  • The values which we have entered. That we need to load to the linked list. 1.First we need to load the values. That is total count for nodes and the node values. 2.Now we need to load the value in the first node.In that the first will taken by the value and the next will be the address. 3.Now we have to give the statement for the next node. Then the link between the two nodes will be performed and the value will be sent to the next. 4.In that way the values will be loaded one by one in to the list.
  • For the two math operations we need to build the program for coparission operation.
  • Then I gave the statement for printing the result values and adresses.

main.c file:

#include"list.h"//declaring the header file
struct node {
int data;
struct node *next;
} *set1, *set2, *unionvalues, *intersectionvalues;//declaring the structure variables.

int main() //starting the main function.
{
int i, set1Count,set2Count, temp;//declaring the variables in the main function.
//Creating First linked List.
printf("Enter number of nodes in first Linked List\n");//printing the statement to enter the value of number of nodes.
fflush(stdout);
scanf("%d", &set1Count);//loading the value.
printf("Enter %d integers\n", set1Count);//print statement to enter the given number of values.
fflush(stdout);
for(i=0; i<set1Count; i++)//for loop operation
{
scanf("%d", &temp);//loading the values in temp.
insert(&set1, temp);//sending the values of temp into set1 address.
}
printLinkedList(set1);//printing the set1 values.
fflush(stdout);
printf("\nEnter number of nodes in second Linked List\n");//printing the statement to enter the value of number of nodes.
fflush(stdout);
scanf("%d", &set2Count);//loading the value.
printf("Enter %d integers\n", set2Count);//print statement to enter the given number of values.
fflush(stdout);
for(i=0; i<set2Count; i++)//for loop operation
{
scanf("%d", &temp);//loading the values in temp.
insert(&set2, temp);//sending the values of temp into set2 address.
}
printLinkedList(set2);//printing the set2 values.
/* printing Union of two given linked list */
unionfun(set1, set2);//getting the values of union function from .c file.
//getting the values of intersection function from .c file.
printf("\nUnion Linked List\n");//printing statement.
printLinkedList(unionvalues);//printing the values.
fflush(stdout);
intersection(set1,set2);
printf("\nIntersection Linked List\n");//printing statement.
printLinkedList(intersectionvalues);//printing the values
fflush(stdout);
return 0;
}
void printLinkedList(struct node *set)//function for printing the values of sets.
{
while (set != NULL) //node values are not equal to NULL then it goes.
{
printf("%d", set->data);//printing thr first value.
set = set->next;//going for the next data.
if(set != NULL)//If the value of node is equal to NULL then the process will stop. The last value of node is NULL.
printf("-->");
}
}

List.c file:

#include "list.h"//librery file
struct node {
int data;
struct node *next;
} *set1, *set2, *unionvalues, *intersectionvalues;//declaring the structure variables.

void insert(struct node **head, int n)//creating the n number of nodes.
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));//node creating syntax.
printf("\n.....inserted element....\n");
printf("addres of created node:%d\n",newNode);
fflush(stdout);
newNode->data = n;//sending the data to the new node.
printf("inserted value %d\n",newNode->data);
fflush(stdout);
newNode->next = *head;//going for a next node of operation.
*head = newNode;//going for a next node of operation.
}
struct node* unionfun(struct node *set1, struct node *set2)//union function declaration.
{
//Add all nodes of first Linked List to unionvalues
struct node *temp = set1;//giving structure as temp is equal to set1.
while(temp != NULL)//if the temp is not equal to NULL then the process will continue.
{
insert(&unionvalues, temp->data);//loading the temp values one by one to union values.
temp = temp->next;//going foy the next value.
}
// Insert those nodes of set2 which is not present in set1
while(set2 != NULL)
{
if(!compare(set1,set2->data))//with this condition the unequal values in the set2 will go.Else it goes for the next value.
{
insert(&unionvalues, set2->data);//that unequal value will go to union values.
}
set2 = set2->next;//to go for the next value compare.
}
return unionvalues;//giving the union values to the main function.
}

//Returns the Linked List which contains common nodes of two given linked list
struct node* intersection(struct node *set1, struct node *set2)//structure for printing the intersection values.
{
intersectionvalues = NULL;
//Search every element of set1 in set2,If found then add it to intersection List.
while(set1 != NULL)//going to the while loop.
{
if(compare(set2, set1->data))//comparing the set2 and set1 values with holding the set2 value as constant.
{
insert(&intersectionvalues, set1->data);//if the value of set 1 is equal to the value of set2 then it goes to intersection .
}
set1 = set1->next;//going for the next value comparing.
}
return intersectionvalues;//giving all values to the main function.
}
int compare(struct node *head, int n)//compare function which is used by the union and intersection.
{
while (head != NULL)//check condition.if satisfied then go.
{
if (head->data == n)//if data in head is equal to the n value then it returns the value in n. Else goes for the next value.
{
return 1;
}
head = head->next;//next value.
}
return 0;
}
//Prints a linked list from head node till tail node.

list.h file:

#ifndef LIST_H_
#define LIST_H_
#include
#include


#endif /* LIST_H_ */

result:

Explanation:

This program

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.