Challenge

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

AIM:

Creation of user defined data type to implement the user interface for working with 'set' using linked list.

SOURCE CODE:

#include<stdio.h>

#include<stdlib.h>

//USER DEFINED DATA TYPE

typedef struct node

{

int data;

struct node* next;

} node;

//CREATING A NEW NODE

node* createnode(int data)

{

node* newnode = (node*)malloc(sizeof(node));

if(!newnode)

{

printf("memory allocation failed\n");

exit(1);

}

newnode->data=data;

newnode->next=NULL;

return newnode;

}

//INSERT INTO SORTED LINKED LIST WITHOUT DUPLICATES

void InsertSorted(node**head,int data)

{

node*current=*head;

node*prev=NULL;

//AVOIDING THE DUPLICATES

while(current!=NULL&&current->data<data)

{

prev=current;

current=current->next;

}

if(current!=NULL&&current->data==data)

return; //HERE DUPLICATE IS FOUND

node*newnode=createnode(data);

if(prev==NULL)

{

newnode->next=*head;

*head=newnode;

}

else

{

newnode->next=prev->next;

prev->next=newnode;

}

}

//DISPLAYING THE LINKED LIST

void Display(node*head)

{

while(head!=NULL)

{

printf("%d ",head->data);

fflush(stdout);

head=head->next;

}

printf("\n");

fflush(stdout);

}

//FUNCTION FOR COMPUTING THE INTERSECTION OF TWO SETS

node*Intersection(node*set1,node*set2)

{

node*result=NULL;

node*temp1=set1;

while(temp1!=NULL)

{

node*temp2=set2;

while(temp2!=NULL)

{

if(temp1->data==temp2->data)

{

InsertSorted(&result,temp1->data);

break;

}

temp2=temp2->next;

}

temp1=temp1->next;

}

return result;

}

//FUNCTION FOR COMPUTING UNION OF TWO SETS

node*UnionSet(node*set1,node*set2)

{

node*result=NULL;

node*temp=set1;

while(temp!=NULL)

{

InsertSorted(&result,temp->data);

temp=temp->next;

}

temp=set2;

while(temp!=NULL)

{

InsertSorted(&result,temp->data);

temp=temp->next;

}

return result;

}

int main()

{

node*set1=NULL;

node*set2=NULL;

int n1,n2,data;

printf("Enter the number of elements in set1: ");

fflush(stdout);

scanf("%d",&n1);

printf("Enter elements of set1:\n");

fflush(stdout);

for(int i=0;i<n1;i++)

{

scanf("%d",&data);

InsertSorted(&set1,data);

}

printf("Enter the number of elements in set2: ");

fflush(stdout);

scanf("%d",&n2);

printf("Enter elements of set2:\n");

fflush(stdout);

for(int i=0;i<n2;i++)

{

scanf("%d",&data);

InsertSorted(&set2,data);

}

printf("\nset1: ");

fflush(stdout);

Display(set1);

printf("set2: ");

fflush(stdout);

Display(set2);

node*inter=Intersection(set1,set2);

printf("\nIntersection of set1 and set2: ");

fflush(stdout);

Display(inter);

node*uni=UnionSet(set1,set2);

printf("Union of set1 and set2: ");

fflush(stdout);

Display(uni);

return 0;

}

OUTPUT:

Enter the number of elements in set1: 5

Enter elements of set1:

10 40 30 20 50

Enter the number of elements in set2: 4

Enter elements of set2:

30 40 60 50

set1: 10 20 30 40 50

set2: 30 40 50 60

Intersection of set1 and set2: 30 40 50

Union of set1 and set2: 10 20 30 40 50 60

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.