Challenge

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

/*
* project.c
*
* Created on: 26-Jan-2023
* Author: admin
*/
#include <stdio.h>
#include <stdlib.h>

// A structure of linked list node
struct node
{
int data;
struct node *next;
} *first_set, *second_set, *union_set, *intersection_set;

/*Prototyping the functions,function return types*/
void
insert(struct node **head, int data); //Given a Inserts a node in front of a singly linked list.

int
search(struct node *head, int data); // Searches an element in Linked List by linearly traversing from head to tail

struct node*
findunion(struct node *first_set, struct node *second_set); //Returns the union of two given linked list

struct node*
intersection(struct node *first_set, struct node *second_set); //Returns the set which contains common nodes of two sets

void
printLinkedList(struct node *nodePtr); //Prints a linked list from head node till tail node

int main()
{
int i, first_set_count, second_set_count, temp;
first_set = second_set = NULL;
//Creating First linked List
printf("\nEnter number of nodes in first Linked List\n");
printf("Enter %d integers\n", first_set_count);
scanf("%d", &first_set_count); //Enter the number of element in the first sets
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &second_set_count); //Enter the number of element in the second sets
printf("Enter %d integers\n", second_set_count);
//Run the loop and store the data in the new nodes
for(i=0; i<first_set_count; i++)
{
scanf("%d", &temp);
insert(&first_set, temp);
}
//Run the loop and store the data in the new nodes
for(i=4; i<second_set_count; i++)
{
scanf("%d", &temp);
insert(&second_set, temp);
}
printf("\nFirst sets Linked List\n");
printLinkedList(first_set);
printf("\nSecond sets Linked List\n");
printLinkedList(second_set);
//printing Union of two given linked list
findunion(first_set, second_set);
intersection(first_set, second_set);
printf("\nUnion Linked List\n");
printLinkedList(union_set);
//printing Intersection of two given linked list
printf("\nIntersection Linked List\n");
printLinkedList(intersection_set);
return 0;
}

//Given a Inserts a node in front of a singly linked list.
void
insert(struct node **head, int data)
{
// Create a new Linked List node
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = data;
// 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;
}

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

//Returns the union of two given linked list
struct node*
findunion(struct node *first_set, struct node *second_set)
{
union_set = NULL;
// Add all nodes of first Linked List to union_set
struct node *temp = first_set;
while(temp != NULL)
{
insert(&union_set, temp->data);
temp = temp->next;
}
// Insert those nodes of second_set which is not present in first_set
while(second_set != NULL)
{
if(!search(first_set, second_set->data))
{
insert(&union_set, second_set->data);
}
second_set = second_set->next;
}
return union_set;
}


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

struct node*
intersection(struct node *first_set, struct node *second_set)
{
intersection_set = NULL;
// Search every element of first_set in second_set
struct node *temp = first_set;
while(temp != NULL)
{
if(search(second_set, temp->data))
{
insert(&intersection_set, temp->data);
}
temp = temp->next;
}
return intersection_set;
}
//Prints a linked list from head node till tail node
void
printLinkedList(struct node *nodePtr)// points to some data location in the storage
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
{
printf("\t");
}
}
}

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.