Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Q1. Program to get the output as the union and intersection of these two linked lists.
Used Modular Programming
union_intersection.h file
/* union_intersection.h
* Created on: 29-Jan-2023 Author: Rajni */
#ifndef UNION_INTERSECTION_H_
#define UNION_INTERSECTION_H_
#include<stdio.h>
#include<stdlib.h>
void createNodeList(int); //function to create the linked list
void sortList(); //function to sort the linked list
int *convertarr(int *); //function to convert linked list to array
void union_fcn(int p1 [],int q [],int m,int n); // function to find union of two linked list
void intersection_fc(int p1[],int q[],int m,int n); // function to find intersection of two linked list
#endif /* UNION_INTERSECTION_H_ */
union_intersection.c file
/* union_intersection.c
* Created on: 29-Jan-2023 Author: Rajni */
#include "union_intersection.h"
struct node {
int value;
struct node *next;
}*stnode;
//Function to create Singly linked list
void createNodeList(int n)
{
struct node *fnode, *t;
int val,i;
stnode = (struct node *)malloc(sizeof(struct node)); //dynamic memory allocation
if(stnode == NULL)
{
printf("Memory can not be allocated");
}
else
{
printf("Input data for node 1:"); //value of first node is taken from user
fflush(stdout);
scanf("%d",&val);
stnode->value = val; //links value field of stnode with val
stnode->next = NULL; // links the address field to NULL
t = stnode;
//creating n nodes and adding to linked list
for(i=2;i<=n;i++)
{
fnode = (struct node *)malloc(sizeof(struct node));
if(fnode == NULL)
{
printf("Memory can not be allocated");
break;
}
else
{
printf("Input data for node %d :", i); //nodes input
fflush(stdout);
scanf("%d",&val);
fnode->value = val; //links the value field of fnode with val
fnode->next = NULL; //links the address field of fnode with NULL
t->next = fnode; //links previous node i.e. t to fnode
t = t->next;
}
}
}
}
//Function to sort the values of linked list
void sortList()
{
struct node *current = stnode;
struct node *index = NULL;
int temp;
if(stnode == NULL)
{
return;
}
else
{
while(current != NULL)
{
index = current->next; //Node index point to node next to current
while(index != NULL)
{
if (current->value > index->value) //checking current node value greater than index value
{
temp = current->value;
current->value = index->value;
index->value =temp;
}
index =index->next;
}
current = current->next;
}
}
}
//Function to convert linked list to array
int *convertarr(int *a)
{
struct node *current = stnode;
int i=0;
if(stnode == NULL)
{
printf("List is Empty");
}
while(current != NULL)
{
a[i] = current->value; //storing value in array ...printf("%d\t",a[i]);
current = current->next; // advances the address position of current node
i++;
}
return a;
}
//Function to find union numbers from two linked list
void union_fcn(int p1 [],int q [],int m,int n)
{
int i;
int m1 = p1[m-1];
int n1 = q[n-1];
int max = 0;
//finding out max number from two linked list
if(m1>n1)
max = m1;
else
max = n1;
int u[max+1]; // creating a new array of size max+1
printf("\n\nUnion:\t");
for (i =0 ;i<(max+1);++i)
{
u[i] = 0; // assigning all elements of array u[] as 0
}
printf("%d\t",p1[0]); // printing first element of p1 array
++u[p1[0]]; // incrementing p1[0]th index of array u[] to 1
// printing first linked list values
for(i=1; i<m;++i)
{
// checking the same numbers is repeated again or not
if(p1[i] != p1[i-1])
{
printf("%d\t",p1[i]);
++u[p1[i]];// incrementing p1[i]th index of array u[] to 1
}
}
// printing second linked list values
for(i=0;i<n;++i)
{
//checking the array u with values 0 or 1..if 0 then second list values will be printed
if(u[q[i]]==0)
{
printf("%d\t",q[i]);
++u[q[i]]; // incrementing q[i]th index of array u[] to 1...to check for repeated numbers
}
}
}
//Function to find intersection numbers from two linked list
void intersection_fc(int p1[],int q[],int m,int n)
{
int i=0, j=0;
printf("\nIntersection:\t");
while(i<m && j<n)
{
//condition check for repeated numbers.
if(i>0 && p1[i]==p1[i-1])
{
++i;
continue;
}
if(p1[i]<q[j]) // p(i) not present in q.. increment i value
{
++i;
}
else if(q[j]<p1[i]) // q(j) not present in p.. increment j value
{
++j;
}
else // p[i]==q[j]..print the number and increment both i and j
{
printf("%d\t",q[j]);
++j;
++i;
}
}
}
main.c file
/* Program to get the output as the union and intersection of these two linked lists.
* main.c
* Created on: 29-Jan-2023 Author: Rajni */
#include "union_intersection.h"
int main()
{
int m,n,i,j,k;
int*p,*q,*p1;
int a[m],b[n];
//Creating 1st Linked List
printf("For 1st Linked List-");
printf("Enter number of nodes: ");
fflush(stdout);
scanf("%d",&m);
createNodeList(m); //calling function to create linked list with m number of nodes
sortList();
p = convertarr(a); //converting linked list to an array
p1= p;
printf("\n1st Linked List :\t");
for (i =0;i<m;i++)
{
printf("%d\t",p1[i]); //printing 1st linked list values
}
//Creating 2nd Linked List
printf("\n\nFor 2nd Linked List-");
printf("Enter number of nodes: ");
fflush(stdout);
scanf("%d",&n);
createNodeList(n); //calling function to create linked list with n number of nodes
sortList();
q = convertarr(b); //converting linked list to an array
printf("\n2nd Linked List :\t");
for (i =0;i<n;i++)
{
printf("%d\t",q[i]); //printing 2nd linked list values
}
union_fcn(p1,q,m,n); // calling union function
intersection_fc(p1,q,m,n); // calling intersection function
return 0;
}
Makefile
all: main.c union_intersection.c
gcc main.c union_intersection.c -o main -I.
Program Output:

PFA(s) containing .c and .h files.