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<stdio.h>
#include<stdlib.h>

//Define self referential structure

struct node{
int data;
struct node* link;

};

//Function for add first Node in link list
struct node* addtoempty(int data)
{
struct node* ptr = malloc(sizeof(struct node));
ptr->data = data;
ptr->link = NULL;
return ptr;

}

//Function for add node at end of the link list


struct node* addatend(struct node* head, int data)
{
struct node* temp = malloc(sizeof(struct node));
temp->data = data;
temp->link =NULL;

struct node* ptr =head;
while( ptr->link != NULL)
ptr=ptr->link;
ptr->link =temp;
return head;

}

// Searches an element in Linked List by linearly traversing from head to tail
int search(struct node *temp, int num)

{
while (temp != NULL)

{
if (temp->data == num)

{
return 1;
}
temp = temp->link;
}
return 0;

}

//function to find the union
struct node* findunion(struct node* temp1,struct node* temp2)
{
struct node* ptr= temp1;
struct node* ptr2=temp2;

while(ptr2 != NULL){

if(!search(ptr, ptr2->data)){

addatend(ptr,ptr2->data);
}
ptr2 = ptr2->link;

}
return ptr;


}

//function to find the intersection
struct node* findintersection(struct node* temp1,struct node* temp2){
struct node* ptr1=temp1;
struct node* ptr2=temp2;
struct node* ptr3=NULL;
while(ptr1 && ptr2){


if (ptr1->data==ptr2->data){


if(ptr3==NULL){
ptr3=addtoempty(ptr1->data);
}
else{
addatend(ptr3,ptr1->data);
}
ptr2=ptr2->link;
ptr1=ptr1->link;
}
else{
if(ptr1->data < ptr2->data){
ptr1=ptr1->link;
}
else
{
ptr2=ptr2->link;
}
}

}

return ptr3;
}

// Function for Print the link list
void print (struct node* head)
{ struct node* temp = head;
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->link;
}
printf("\n");

}

// main Function
int main()
{
struct node* head1 = NULL;
struct node* head2 = NULL;
struct node* inter =NULL;
struct node* uni =NULL;

head1= addtoempty(1);
addatend(head1,2);
addatend(head1,3);
addatend(head1,4);
addatend(head1,6);

head2= addtoempty(2);
addatend(head2,4);
addatend(head2,6);
addatend(head2,8);

printf("first Link List\n ");
print(head1);
printf("second Link List\n ");
print(head2);

inter=findintersection(head1,head2);

uni=findunion(head1,head2);

printf("Inter section\n ");
print(inter);
printf("union section\n ");
print(uni);

return 0;
}

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.