Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List:
step1: we have create a 2 link list with number of nodes, a node contain two parts one is data and another one is address of the next data with standard instruction
step2: inserting a nodes to two link list where here i have use the push and insert function to insert the node to the two link list
step3: arr[j] is the 1 st linked list data has stored in the array,arr1[m] is the 2nd linked list data has stored in the array
step4:after insertion the two link list had merge them and find the union and intersection to find this the two array of the linked list is comparing and find the duplicate value as one time in union and duplicate value as output in intersection
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *nextptr;
}*stnode1,*stnode2;
void createnodelist(int n1,int n2);
void push(struct node** head_ref, int newdata);
void insert(struct node** head_ref, int newdata);
void uni(int head_ref1,int head_ref2,int n1,int n2);
int main()
{
int n1,n2,n3,n4;
scanf("%d",&n1);
printf("No of node1 in linked list: %d",n1);
scanf("%d",&n2);
printf("\n");
printf("No of node2 in linked list: %d",n2);
createnodelist(n1,n2);
}
void createnodelist(int n1,int n2)
{
int n3,n4;
n3=n1;
n4=n2;
struct node *fnnode,*tmp;
int num,i;
stnode1 = (struct node *)malloc(sizeof(struct node));
stnode2 = (struct node *)malloc(sizeof(struct node));
if(stnode1 == NULL) //check whether the fanode is MULL and if so no memory allocation
{
printf("Memory can not be allocated.");
}
else
{
scanf("%d", &num);
printf("Input data for node 1 : %d ",num);
printf("\n");
stnode1->num = num;
stnode1->nextptr = NULL; // Lines the address field to NULL
tmp=stnode1;
// Creating n nodes and adding to linked list
for (i=2; i<=n1; i++)
{
fnnode = (struct node *)malloc(sizeof(struct node));
if(fnnode == NULL)
{
printf("Memory can not be allocated.");
break;
}
else
{
scanf("%d", &num);
printf("Input data for node1 : %d ",num);
printf("\n");
fnnode->num = num; // links the num field of fallode with num
fnnode->nextptr = NULL; // Links the address field of fallode with MULL
tmp->nextptr = fnnode; // Links previous node i.e. tmp to the fallode
tmp = tmp->nextptr;
}
}
push(&stnode1,10);
}
if(stnode2 == NULL) //check whether the fanode is MULL and if so no memory allocation
{
printf("Memory can not be allocated.");
}
else
{
scanf("%d", &num);
printf("Input data for node 2 : %d",num);
printf("\n");
stnode2->num = num;
stnode2->nextptr = NULL; // Lines the address field to NULL
tmp=stnode2;
// Creating n nodes and adding to linked list
for (i=2; i<=n2; i++)
{
fnnode = (struct node *)malloc(sizeof(struct node));
if(fnnode == NULL)
{
printf("Memory can not be allocated.");
break;
}
else
{
scanf("%d", &num);
printf("Input data for node2 : %d",num,"\n");
printf("\n");
fnnode->num = num; // links the num field of fallode with num
fnnode->nextptr = NULL; // Links the address field of fallode with MULL
tmp->nextptr = fnnode; // Links previous node i.e. tmp to the fallode
tmp = tmp->nextptr;
}
}
insert(&stnode2,12);
}
uni(stnode1,stnode2,n3,n4);
}
void push(struct node** head_ref, int newdata)
{
/* allocate node */
struct node* new_node = (struct node*)malloc(sizeof(struct node));
/* put in the data */
new_node->num = newdata;
/* link the old list of the new node */
new_node->nextptr = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
printf("Insert a Node into the first linked link");
if(*head_ref != 0)
{
while (new_node != NULL) {
printf("%d ", new_node->num);
new_node = new_node->nextptr;
printf("-->");
}
printf("NULL\n");
}
}
void insert(struct node** head_ref, int newdata)
{
/* allocate node */
struct node* new_node = (struct node*)malloc(sizeof(struct node));
/* put in the data */
new_node->num = newdata;
/* link the old list of the new node */
new_node->nextptr = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
printf("Insert a Node into the second linked link");
if(*head_ref != 0)
{
while (new_node != NULL) {
printf("%d ", new_node->num);
new_node = new_node->nextptr;
printf("-->");
}
printf("NULL\n");
}
}
void uni(int stnode1,int stnode2,int n3,int n4){ // both data and address of the linked link
int j=0;
int num = n3+n4+2;
int x,y;
int count,m=0;
int arr[n3];
int arr1[n4];
int arr2[num];
struct node* newnode = stnode1;
struct node* newnode1 = stnode2;
// To get first linked list in an array
while(newnode != NULL) {
arr[j] = newnode->num;
newnode = newnode->nextptr;
// printf("%d",arr[j]);
j++;
}
// To get second linked list in an array
while(newnode1 != NULL) {
arr1[m] = newnode1->num;
newnode1 = newnode1->nextptr;
// printf("%d",arr1[m]);
m++;
}
//To print first linked list
for(int i=0;i<n3+1;i++){
printf("%d",arr[i]);
}
printf("\n");
//To print first linked list
for(int i=0;i<n4+1;i++){
printf("%d",arr1[i]);
}
printf("\n");
printf("\n");
//Merge two arrays
for (x = 0; x < n3+1; x++) {
arr2[x] = arr[x];
}
for ( x = 0 , y = n3+1;
y < num && x < n4+1; x++, y++) {
arr2[y] = arr1[x];
}
// To print merge array
for (x = 0; x < num; x++) {
printf("%d -->", arr2[x]);
}
printf("\n");
// Intersection
for(int a = 0;a<=num;a++){
for(int b=a+1;b<=num;b++){
if(arr2[a]==arr2[b]){
printf("Intersection Values: %d",arr2[a]);
}
}
}
printf("\n");
// Union
for(int a = 0;a<=num;a++){
for(int b=a+1;b<=num;b++){
if(arr2[a]==arr2[b]){
count=1;
}
}
if(count == 0){
printf("%d",arr2[a]);
printf("-->");
}
count=0;
}
}
