Challenge

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

#include
#include

struct node{
struct node*next;
int data;
};
struct node * createList(int );
void print(struct node * );
struct node * getUnion(struct node *, struct node * );
struct node * getintersection(struct node *, struct node *);
struct node * mergeLL(struct node *, struct node * );
struct node * sortLL(struct node *);
int main() {
struct node * L1 = NULL;
struct node * L2 = NULL;
L1 = createList(1);
L2 = createList(2);
printf("List 1 : ");
print(L1);
printf("nList 2 : ");
print(L2);
printf("nIntersection of two linked lists 1 & 2: ");
getintersection(L1, L2);
printf("nUnion of two linked lists 1 & 2 : ");
getUnion(L1, L2);


return 0;
}
struct node * createList(int listNum){
struct node * list = NULL;
struct node * list_tail = NULL;
printf("Enter elements of List-%d n",listNum);
char ch ;
do{
int data;
printf("Enter element : ");
scanf("%d",&data);
struct node * newNode = (struct node *) malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(list == NULL){
list = list_tail = newNode;
}
else{
list_tail->next = newNode;
list_tail = list_tail->next;
}
printf("Would you like to insert another element [Y/N] : ");
scanf(" %c",&ch);
}while(ch == 'y' || ch == 'Y');

return list;
}

void print(struct node * list){
if(list == NULL){
printf("Empty Listn");
return;
}
while(list!=NULL){
printf("%d ",list->data);
list = list->next;
}
printf("n");
}

struct node * getUnion(struct node * L1, struct node * L2){

struct node * merg, * sorting, *i, *j, *L3;
L3= mergeLL(L1,L2);
sortLL(L3);
for(i=L3;i!=NULL;i=i->next){
for(j=i->next;j!=NULL;j=j->next){
if(j->next==NULL){
printf("%d, ",j->data);
}

if(i->data == j->data){

break;
}
else
{
printf("%d, ",i->data);
break;
}

}
}


}

struct node * mergeLL(struct node * L1, struct node * L2){

struct node * temp;
temp=L1;

while(temp->next!=NULL){
temp= temp->next;
}
temp->next= L2;
return L1;

}

struct node * sortLL(struct node *L1){

struct node * i, * j;
int tmp;
for(i=L1;i!=NULL;i=i->next){
for(j=i->next;j!=NULL;j=j->next){
if(i->data > j->data){
tmp=i->data;
i->data= j->data;
j->data=tmp;
}

}

}
}

struct node * getintersection(struct node *L1, struct node *L2){
struct node *n, *p;
for(n=L1;n!=NULL;n=n->next){
for(p=L2;p!=NULL;p=p->next){
if(n->data==p->data){
printf("%d, ",n->data);
break;
}
}
}

}

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.