Challenge

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

//Solution for the question

/*
* miniproj1.c
* Program: Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
* Created on: 16-Oct-2022
* Author: Rakesh VS
*/

#include
#include

// A Linked List Nodes
struct node
{
int data;
struct node *next;
}*FirstList, *SecondList, *UnionList, *IntersectList;


// Function to insert a new node at the beginning of the linked list
void push(struct node **head, int num)
{
// Create a new Linked List node
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = num;
// Next pointer of new node will point to head node of linked list
newNode->next = *head;
//make new node as new head of linked list
*head = newNode;
}

//Function to find an element in Linked List by linearly traversing from head to tail
int find(struct node *head, int num)
{
while (head != NULL)
{
if (head->data == num)
{
return 1;
}
head = head->next;
}
return 0;
}

//Function to return the union of two given linked list
struct node* makeunion(struct node *FirstList, struct node *SecondList)
{
UnionList = NULL;
// Add all nodes of first Linked List to UnionList
struct node *temp = FirstList;
while(temp != NULL)
{
push(&UnionList, temp->data);
temp = temp->next;
}

//To insert those nodes of SecondList which is not present in FirstList
while(SecondList != NULL)
{
if(!find(FirstList, SecondList->data))
{
push(&UnionList, SecondList->data);
}
SecondList = SecondList->next;
}
return UnionList;
}


//Function to return the Linked List which contains common nodes of two given linked list
struct node* makeintersect(struct node *FirstList, struct node *SecondList)
{
IntersectList = NULL;
//find every element of FirstList in SecondList, If found then add it to intersection List
while(FirstList != NULL)
{
if(find(SecondList, FirstList->data))
{
push(&IntersectList, FirstList->data);
}
FirstList = FirstList->next;
}
return IntersectList;
}


// Function to print a given linked list
void PrintList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d->", nodePtr->data);
nodePtr = nodePtr->next;
}
}

int main()
{
int i;
int x[] = {1, 2, 3, 4, 5};
int y[] = {3, 4, 5, 6, 7};

int m = sizeof(x)/sizeof(x[0]);
int n = sizeof(y)/sizeof(y[0]);

printf("\nFirst linked list:\t");
// construct the first linked list
for(i=m-1; i>=0; i--)
{
push(&FirstList, x[i]);
}
PrintList(FirstList);

printf("\nSecond linked list:\t");
// construct the second linked list
for(i=n-1; i>=0; i--)
{
push(&SecondList, y[i]);
}
PrintList(SecondList);

//printing Union of two given linked list
makeunion(FirstList, SecondList);
makeintersect(FirstList, SecondList);
printf("\nUnion of two linked lists:\t");
PrintList(UnionList);
printf("\nIntersection of two linked lists:\t");
PrintList(IntersectList);

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.