Challenge

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

/* algorithm of the program for getting union and intersection of two sets :
Step-1: taking number of elements in set-A, creating node, taking element and storing element in each node, also storing base address of set-A
Step-2: taking number of elements in set-B, creating node, taking element and storing elements in each node, also storing base addess of set-B
step-3: creating union and intersection set and also storing base address of union set and intersection set
step-4: display elements of set-A and set-B by using their base addresses
step-5: deleting nodes of repeated numbers from both union set and intersection set and displaying remaining elements on to console
*/

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

struct node{ //creating user defined datatype using struct to create node//
int value;
struct node *next;
};

//prototypes of functions used//
void createSet(int n); //function for creating set-A and set-B linked list, by taking number of elements as function parameter//
void displayList(struct node *start); //function for displaying created set linked list, by taking base address of respective set//
void displayinsertion(struct node *newstart); //function for deleting repeated values and displaying union and intersection list on console//
void insertingof(); //function for creating union and intersection set//

int a,b; //declaration of global variables of integer data type to store number of set elements //
struct node *first, *Set_A, *Set_B, *ufirst, *ifirst; // declaration of global variables of struct node data type for storing base addresses of sets//

void createSet(int n) //function for taking respective number of elements and creating set-A and Set-B//
{
struct node *second, *temp; //declaration of local variables of struct node data type, used for creating list//
int num; // declaration of local variable of integer data type to store set elements temporarily//
first = (struct node*)malloc(sizeof(struct node)); //dynamically allocating memory for first node of the Set//
if(first==NULL) //checking for memory allocated or not//
{
printf("Memory cannot be allocated");
}
else
{
printf("Enter the element 1: "); //asking for the first element of the set and storing it in the variable 'num'//
fflush(stdout);
scanf("%d",&num);
first->value = num; //storing the 'num' value to the value part of the first node//
first->next = NULL; //storing address part of the first node as NULL//
temp = first; //copying the base address of Set to local variable 'temp' so it can be traversed keeping the base address safe//

for(int i=2;i<=n;i++) //loop for creating next nodes of Set and linking them//
{
second = (struct node*)malloc(sizeof(struct node)); //allocating memory to elements recursively on after the other//
if(second == NULL)
{
printf("Memory cannot be allocated");
}
else
{
printf("Enter the element %d: ",i); //asking for the element and storing in allocated memory//
fflush(stdout);
scanf("%d",&num);
second->value = num;
second->next = NULL;
temp->next = second;
temp = temp->next;

}
}
}
}


void insertingof() //function for creating union and intersection linked list//
{
struct node *unode, *satmp, *sbtmp, *utmp, *inode, *itmp;
//'unode' for allocating memory to union set//
//'satmp' for traversing elements of set-A//
//'sbtmp' for traversing elements of set-B//
//'utmp' for traversing elements of union//
//'inode' for allocating memory to intersection set//
//'itmp' for traversing elements of intersection//
satmp = Set_A;
int j = 0;

if(Set_A == NULL)
{
printf("List is empty\n");
}
else
{
ufirst = (struct node*)malloc(sizeof(struct node)); //creating and storing first node of union set and copying base address of first node to 'utmp'//
ufirst->value = satmp->value;
ufirst->next = NULL;
utmp = ufirst;

for(int i=2;i<=a;i++) //loop for copying the other elements to union set//
{
satmp = satmp->next;
unode = (struct node*)malloc(sizeof(struct node));
unode->value = satmp->value;
unode->next = NULL;
utmp->next = unode;
utmp = utmp->next;
}
satmp = Set_A;
sbtmp = Set_B;

while(sbtmp != NULL) // loop for inserting of set-B elements to union set and intersection set based on the condition//
{
while(satmp != NULL)
{
if(sbtmp->value != satmp->value) //checking set-B element is present in set or not//
{
satmp = satmp->next;
continue;
}
else //if set-B element is present in set-A it is copied to insertion set//
{
if(j==0)
{
inode = (struct node*)malloc(sizeof(struct node));
inode->value = sbtmp->value;
inode->next = NULL;
ifirst = inode;
itmp = inode;
j++;
}
else
{
inode = (struct node*)malloc(sizeof(struct node));
inode-> value = sbtmp->value;
inode->next = NULL;
itmp->next = inode;
itmp = itmp->next;
j++;
}
sbtmp = sbtmp->next;
satmp = Set_A;
continue;

}
}
// if the set-B element is not there in set-A list, a node is created in union set and set-B element is copied into it//
unode = (struct node*)malloc(sizeof(struct node));
unode-> value = sbtmp->value;
unode->next = NULL;
utmp->next = unode;
utmp= utmp->next;
sbtmp = sbtmp->next;
satmp = Set_A;
}
}
}


void displayList(struct node *start) // function for displaying elements in Set-A and Set-B by taking the base address of the set as function parameter//
{
struct node *tmp;
if(start == NULL)
{
printf("List is empty:");
}
else
{
tmp = start;
while(tmp != NULL)
{
printf(" %d ",tmp->value);
tmp = tmp->next;
}
printf("\n");
}
}


// 'displayinsertion' function takes base address of union and intersection set as function parameter one after the other and display elements in them //
void displayinsertion(struct node *newstart) //function for displaying elements of union and intersection set after deleting repeated values//
{
struct node *tmp, *dtmp, *dfirst, *dsecond; //declaring local variables of struct node data type //
dtmp = newstart;
dfirst = dtmp;
dsecond = dfirst->next;

while(dtmp != NULL) // checking for repeated elements from union and intersection set's and deleting the excess nodes//
{
while(dsecond !=NULL)
{
if(dtmp->value == dsecond->value)
{
dfirst->next = dsecond->next;
dsecond = dsecond->next;
continue;
}
else
{
dsecond = dsecond->next;
dfirst = dfirst->next;
continue;
}
}
dtmp = dtmp->next;
dfirst = dtmp;
dsecond = dfirst->next;
if(dtmp->next == NULL)
{
break;
}
}

if(newstart == NULL) //displaying elements of union and intersection set's//
{
printf("List is empty:");
}
else
{
tmp = newstart;
while(tmp != NULL)
{
printf(" %d ",tmp->value);
tmp = tmp->next;
}
printf("\n");
}
}

int main()
{

printf("Enter the number of elements in Set A: "); //asking the number of elements in Set_A//
fflush(stdout);
scanf("%d",&a); //taking the number and storing it in global variable 'a'//
createSet(a); //calling createSet function to create Set-A linked list, by passing number of elements as function parameter//
Set_A = first; //storing the base address of created Set_A linked list in 'Set_A' global variable//

printf("\nEnter the number of elements in Set B: ");//asking the number of elements in Set_B//
fflush(stdout);
scanf("%d",&b); //taking the number and storing it in global variable 'b'//
createSet(b); //calling 'createSet' function to create Set-B linked list, by passing number of elements as function parameter//
Set_B = first; //storing the base address of created Set_B linked list in 'Set_B' global variable//

insertingof(); //calling 'insertingof' function to create union and intersection linked list//

printf("\n\nElements in Set A: ");
displayList(Set_A); //calling 'displayList' function for displaying elements of set-A on console by passing base address of 'set_A' as function parameter//
printf("\nElements in Set B: ");
displayList(Set_B); //calling 'disaplayList' function for displaying elements of set-B on console by passing base address of 'set_B' as function parameter//

printf("\nThe elements in union set are: ");
displayinsertion(ufirst); //calling 'displayInsertion' function for displaying elements of union set on to console by passing base address of union as function parameter//

printf("\nThe elements in intersection set are: ");
displayinsertion(ifirst); //calling 'displayInsertion' function for displaying elements of intersection set on to console by passing base address of intersection as function parameter//

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.