Challenge

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

Implementation of interface for mathematical Set Theory using C programming:

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

//A Structure of Linked list node
struct node
{
int data;
struct node *next; //Points to the next node address of a Linked list
}; //Structure members

struct node *head1, *head2, *head, *tail, *unionLL, *intersectionLL; //Struct node Variables

void initialize() //Function definition with no arguments and no return
{
head1 = head2 = NULL; //Initializes head with null value
}

//Insert Function to insert node in front of a singly Linked list
void insert_node(struct node **head, int num) //Function definition with arguments and no return
{
struct node* newNode = (struct node*) malloc(sizeof(struct node)); //Creats a new node in Linked List and allocates memory
newNode->data = num; //Passing input element to new node data
newNode->next = *head; //Next pointer of new node will point to head node
*head = newNode; //Make new node as new head of Linked list
}

//Compare Function to compare an element in Linked List by linearly traversing from head to tail
int compare(struct node *head, int num) //Function definition with arguments and with return
{
while (head != NULL) //While loop to check head not equal to null value
{
if (head->data == num) //if condition
{
return 1; //Return value
}
head = head->next; //Next pointer of head node points to head node
}
return 0; //Returns null
}

//Union Function to print all nodes of two given Linked lists without repetition
struct node* get_union(struct node *head1, struct node *head2) //Function definition with arguments and with return
{
unionLL = NULL; //Passing null value to Union Linked list
//Add all nodes of first Linked List to Union Linked list
while(head1 != NULL) //While loop to check head not equal to null value
{
insert_node(&unionLL, head1->data); //Insert function call to insert first list nodes to Union list
head1 = head1->next; //Passing head1 next node to head1 node
}

//Insert the nodes of head2 which are not present in head1
while(head2 != NULL) //while loop to check head not equal to null value
{
if(!compare(unionLL, head2->data)) //Compares head2 nodes with Union list nodes using compare function call
{
insert_node(&unionLL, head2->data); //Insert function call to insert into union list when not matching with first list
}
head2 = head2->next; //Passing head2 next node to head2 node
}
return unionLL; //Returns Union Linked list
}

//Intersection Function to print the common nodes of two given Linked lists
struct node* get_intersection(struct node *head1, struct node *head2) //Function definition with arguments and with return
{
intersectionLL = NULL; //Passing null value to Intersection Linked list
//Compares every element of head1 in head2, if matches then add it to intersection List
while(head1 != NULL) //While loop to check head not equal to null value
{
if(compare(head2, head1->data)) //Condition to compare head1 & head2 nodes by using Compare function call
{
insert_node(&intersectionLL, head1->data); //Insert function call to insert into Intersection list when nodes are matching
}
head1 = head1->next; //Passing head1 next node to head1 node
}
return intersectionLL; //Returns Intersection list
}

//Display Function for printing the Linked list nodes
void display_list(struct node *head) //Function definition with arguments and no return value
{
if(head==NULL) //if condition to check head equals to null or not
{
printf("List is Empty"); //Prints output in console
}
else //else condition
{
tail = head; //Assigning head node to tail temporarily
while (tail != NULL) //while loop
{
printf("%d", tail->data); //Prints the head node data
tail = tail->next; //Next pointer of head node points to head node
if(tail != NULL) //if condition
printf("-->"); //Prints output in console
}
}
}

int main() //Main Function
{ //Open loop
int i, first_total, second_total, firstlist_value, secondlist_value; //Local Variable declaration
initialize(); //Caller Function

//Creating First Linked List
printf("Enter number of nodes in first Linked List: "); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%d", &first_total); //Reads input from console
printf("Enter %d nodes to first Linked list:\n", first_total); //Prints output in console
for(i=0; i<first_total; i++) //for loop to read the nodes given by the user
{
fflush(stdout); //Clears buffer
scanf("%d", &firstlist_value); //Reads input from console
insert_node(&head1, firstlist_value); //Insert function call to insert nodes into first list
}
display_list(head1); //Display function call to display the first list nodes

//Creating Second Linked List
printf("\nEnter number of nodes in second Linked List: "); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%d", &second_total); //Reads input from console
printf("Enter %d nodes to second Linked list:\n", second_total); //Prints output in console
for(i=0; i<second_total; i++) //for loop to read the nodes given by the user
{
fflush(stdout); //Clears buffer
scanf("%d", &secondlist_value); //Reads input from console
insert_node(&head2, secondlist_value); //Insert function call to insert nodes into second list
}
display_list(head2); //Display function call to display the second list nodes

get_union(head1, head2); //Union function call to perform union operation
get_intersection(head1, head2); //Intersection function call to perform intersection operation

//Output of Union Linked list
printf("\nUnion of two given Linked Lists is:\n"); //Prints Union nodes of two given Linked lists
display_list(unionLL); //Display function call to display the Union Linked list

//Output of Intersection Linked list
printf("\nIntersection of two given Linked Lists is:\n"); //Prints Intersection nodes of two given Linked lists
display_list(intersectionLL); //Display function call to display the Intersection Linked list

return 0; //Returns null
} //Close loop

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.