Challenge

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

#include
#include
struct node
{
int data;
struct node*next;
}*LLone,*LLTwo,*unionLL,*intersectionLL;
void initilize()
{
LLone=LLTwo=NULL;
}
void insert(struct node**head,int num)
{
struct node*newnode=(struct node*)malloc(sizeof(struct node));//create a new linked list node//
newnode->data=num;
newnode->next=*head;//next node of new node will point out to head node of LL//
*head=newnode;
}
int search(struct node*head,int num)//searches the element in LL by linearly traversing from head to tail//
{
while(head!=NULL)
{
if(head->data==num)
{
return 1;
}
head=head->next;
}
return 0;
}
struct node*findunion(struct node*LLOne,struct node*LLTwo)//returns the union of two given linked list//
{
unionLL=NULL;
struct node*temp=LLOne;
while(temp!=NULL)
{
insert(&unionLL,temp->data);
temp=temp->next;
}
while(LLTwo!=NULL)
{
if(!search(LLOne,LLTwo->data))
{
insert(&unionLL,LLTwo->data);
}
LLTwo=LLTwo->next;
}
return unionLL;
}
struct node*intersection(struct node*LLOne,struct node*LLTwo)
{
intersectionLL=NULL;
while(LLOne!=NULL)
{
if(search(LLTwo,LLOne->data))
{
insert(&intersectionLL,LLOne->data);
}
LLOne=LLOne->next;
}
return intersectionLL;
}
void printlinkedlist(struct node *nodePtr)
{
while(nodePtr!=NULL)
{
printf("%d",nodePtr->data);
nodePtr=nodePtr->next;
if(nodePtr!=NULL)
printf(" ");
}
}
int main()
{
int i,LLOnecount,LLTwocount,temp;
initilize();
//creating first ll//
printf("Enter number of nodes in first linked listn");
scanf("%d",&LLOnecount);
printf("Enter %d integersn",LLOnecount);
for(i=0;i {
scanf("%d",&temp);
insert(&LLone,temp);
}
printlinkedlist(LLone);
printf("nEnter number of nodes in second linked listn");
scanf("%d",&LLTwocount);
printf("Enter %d integersn",LLTwocount);
for(i=0;i {
scanf("%d",&temp);
insert(&LLTwo,temp);
}
printlinkedlist(LLTwo);
findunion(LLone,LLTwo);
intersection(LLone,LLTwo);
printf("nnunion linked listn");
printlinkedlist(unionLL);
printf("nnintersection linked listn");
printlinkedlist(intersectionLL);
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.

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.