Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/*
* main.c
*
* Created on: 23-Nov-2022
* Author: Leena Theresa S
* Description: Creation of user defined data type to implement the user interfaces for
* working with 'Set Mathematical Theory' using Linked List
*/
#include <stdio.h> //Permission to use printf and scanf functions
#include <stdlib.h> //To use malloc function for allocating the memory dynamically
typedef struct node //Data type node having two part named as list
{
int data; //Data is an Integer
struct node *link; //Link pointer points to the next node of type node structure
}list;
list *head[2]; //Array of structure for getting the base address of two linked list
list *uni = NULL; //Storing the base address of combined lists
list *intersection = NULL; //Creating the list for common elements in two lists
void Create_List(int,int); //Prototype for Creating the list with argument
void Display_List(list *); //Function Declaration
void Union_List();
void Intersection_List(); //To tell the compiler that, I am gonna create these functions
int main() //Execution starts from here
{
int n; //Variable to store the total no.of elements in the list
for (int i=1;i<=2;i++) //Iteration to create two Linked Lists
{
printf("Enter the number of nodes in the %d list :",i);
fflush(stdout);
scanf("%d",&n); //Reading the total no.of elements in the list
Create_List(n,i); //Creating the list with total no.of elements and were to store
}
printf("\n");
for (int i=1;i<=2;i++) //Iteration to print the two Linked List
{
printf("%d list nodes are : ",i);
fflush(stdout);
Display_List(head[i]); //Displaying the list with the base address of the list
}
printf("\nCommon nodes of two list is : ");
Intersection_List(); //Calling the function to get the same elements in two lists
Union_List(); //Calling the function to concatenate the Linked lists
return 0;
}
void Create_List(int n,int i) //Function Definition will have the logic for creating the list
{
list *temp = NULL,*p = NULL; //Local variables will destroy after the function execution
for (int j=1;j<=n;j++) //Creating n no.of nodes in one list
{
temp = (list *)malloc (sizeof(list)); //Allocating the memory for a node
printf("Enter %d node data :",j);
fflush(stdout);
scanf("%d",&temp->data); //Reading the node data from the user
temp->link = NULL; //Assigning NULL to the node address part
if (head[i]== NULL) //For first node creation in the list
{
head[i] = temp;
}
else
{
p = head[i];
while(p->link != NULL) //For further nodes added at the end of the lists
{
p = p->link; //Control move to the end of the list
}
p->link = temp; //Adding the node at the end of the list
}
}
}
void Display_List (list * root) //Function Definition will have the Base address of the list
{
list *p = root; //Assigning the base address to the local variable
while (p != NULL) //Iteration for Printing the node data till the last node
{
printf("%d ",p->data);
fflush(stdout);
p=p->link; //Moving to the next node
}
printf("\n");
fflush(stdout); //To set the output buffer as zero
}
void Union_List () //Logic to combine two Linked lists
{
list *p = NULL; //Local variable to traverse the list
p = head[1]; //Assigning the first list to the local variable
uni = p; //Store the base address of the local variable to combine the lists
while (p->link != NULL)
{
p = p->link; //To move to the last node of the first list
}
p->link = head[2]; //Adding the base address of the second list at the last node link
printf("Combined form of lists is : "); //part of the first lists
fflush(stdout);
Display_List(uni); //Calling the function with base address to print the combined list
}
void Intersection_List() //Logic to find the same node data in the two Linked Lists
{
list *temp,*r; //Local variables to create the list containing same data in lists
for (list *p = head[1]; p != NULL; p = p->link) //Traversing the First lists elements
{
for(list *q = head[2]; q != NULL; q = q->link ) //Traversing the second lists
{
if(p->data == q->data) //Check whether the data is same or not
{
temp = (list *)malloc (sizeof(list)); //Allocating the memory dynamically
temp->data = p->data; //Assigning the data to the node
temp->link = NULL; //Node link having NULL
if (intersection == NULL)
{
intersection = temp; //For first node creation in the list
}
else
{
r = intersection; //For further node creation in the lists
while(r->link != NULL)
{
r = r->link; //Control move to the last node
}
r->link = temp; //Adding node at the end of the list
}
}
}
}
Display_List(intersection); //Calling with base address of the newly created lists
}