Here's the implementation:
/*
Problem Description:
Intersection of two sorted list
submitted by
Arjun Mishra
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//structure of node having elements of list1
struct node1
{
int data;
struct node1 *next;
}*head1,*tail1,*temp1;
//structure of node having elements of list2
struct node2
{
int data;
struct node2 *next;
}*head2,*tail2,*temp2;
//structure of node having elements of intersect
struct node
{
int data;
struct node *next;
}*head,*tail,*temp;
//utility to insert the elemnets in list1
void push1(int val)
{
temp1=(struct node1*)malloc(sizeof(struct node1));
if(!temp1)
{
printf("Error:unable to allocate memory\n");
return;
}
temp1->data=val;
temp1->next=NULL;
if(head1==NULL)
{
head1=temp1;
tail1=temp1;
}
else
{
tail1->next=temp1;
tail1=temp1;
}
}
//utility to inesert the elements in list2
void push2(int val)
{
temp2=(struct node2*)malloc(sizeof(struct node2));
if(!temp2)
{
printf("Error:unable to allocate memory\n");
return;
}
temp2->data=val;
temp2->next=NULL;
if(head2==NULL)
{
head2=temp2;
tail2=temp2;
}
else
{
tail2->next=temp2;
tail2=temp2;
}
}
//utility push the intersected elements
void push(int val)
{
temp=(struct node*)malloc(sizeof(struct node));
if(!temp)
{
printf("Error:unable to allocate memory\n");
return;
}
temp->data=val;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
//utility to traverse the intersected linked list
void traverse1()
{
if(head1==NULL)
{
cout<<"No intersection!\n";
return;
}
temp1=head1;
while(temp1!=NULL)
{
cout<<temp1->data<<"->";
temp1=temp1->next;
}
cout<<"NULL"<<endl;
}
//utility to traverse the intersected linked list
void traverse2()
{
if(head2==NULL)
{
cout<<"No intersection!\n";
return;
}
temp2=head2;
while(temp2!=NULL)
{
cout<<temp2->data<<"->";
temp2=temp2->next;
}
cout<<"NULL"<<endl;
}
//utility to traverse the intersected linked list
void traverse()
{
if(head==NULL)
{
cout<<"No intersection!\n";
return;
}
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
//utility to find the intersection
void findIntersection(struct node1 *head1,struct node2* head2)
{
if(head1==NULL||head2==NULL)
{
head=NULL;
return;
}
temp1=head1;
temp2=head2;
while(temp1!=NULL&&temp2!=NULL)
{
if(temp1->data==temp2->data)
{
push(temp1->data);
temp1=temp1->next;
temp2=temp2->next;
}
else if(temp1->data<temp2->data)
temp1=temp1->next;
else
temp2=temp2->next;
}
//calling traverse function to traverse the list
cout<<"resultantList :\t";
traverse();
}
int main()
{
//inserting elements in list1
push1(1);
push1(2);
push1(3);
push1(4);
push1(6);
cout<<"List 1 :\t";
traverse1();
cout<<endl<<endl;
//inserting elements in list2
push2(2);
push2(4);
push2(6);
push2(8);
cout<<"List 2 :\t";
traverse2();
cout<<endl<<endl;
//calling the function findIntersect to find the intersection
findIntersection(head1,head2);
return 0;
}
/*
Problem Description:
Intersection of two sorted list
submitted by
Arjun Mishra
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//structure of node having elements of list1
struct node1
{
int data;
struct node1 *next;
}*head1,*tail1,*temp1;
//structure of node having elements of list2
struct node2
{
int data;
struct node2 *next;
}*head2,*tail2,*temp2;
//structure of node having elements of intersect
struct node
{
int data;
struct node *next;
}*head,*tail,*temp;
//utility to insert the elemnets in list1
void push1(int val)
{
temp1=(struct node1*)malloc(sizeof(struct node1));
if(!temp1)
{
printf("Error:unable to allocate memory\n");
return;
}
temp1->data=val;
temp1->next=NULL;
if(head1==NULL)
{
head1=temp1;
tail1=temp1;
}
else
{
tail1->next=temp1;
tail1=temp1;
}
}
//utility to inesert the elements in list2
void push2(int val)
{
temp2=(struct node2*)malloc(sizeof(struct node2));
if(!temp2)
{
printf("Error:unable to allocate memory\n");
return;
}
temp2->data=val;
temp2->next=NULL;
if(head2==NULL)
{
head2=temp2;
tail2=temp2;
}
else
{
tail2->next=temp2;
tail2=temp2;
}
}
//utility push the intersected elements
void push(int val)
{
temp=(struct node*)malloc(sizeof(struct node));
if(!temp)
{
printf("Error:unable to allocate memory\n");
return;
}
temp->data=val;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
//utility to traverse the intersected linked list
void traverse1()
{
if(head1==NULL)
{
cout<<"No intersection!\n";
return;
}
temp1=head1;
while(temp1!=NULL)
{
cout<<temp1->data<<"->";
temp1=temp1->next;
}
cout<<"NULL"<<endl;
}
//utility to traverse the intersected linked list
void traverse2()
{
if(head2==NULL)
{
cout<<"No intersection!\n";
return;
}
temp2=head2;
while(temp2!=NULL)
{
cout<<temp2->data<<"->";
temp2=temp2->next;
}
cout<<"NULL"<<endl;
}
//utility to traverse the intersected linked list
void traverse()
{
if(head==NULL)
{
cout<<"No intersection!\n";
return;
}
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
//utility to find the intersection
void findIntersection(struct node1 *head1,struct node2* head2)
{
if(head1==NULL||head2==NULL)
{
head=NULL;
return;
}
temp1=head1;
temp2=head2;
while(temp1!=NULL&&temp2!=NULL)
{
if(temp1->data==temp2->data)
{
push(temp1->data);
temp1=temp1->next;
temp2=temp2->next;
}
else if(temp1->data<temp2->data)
temp1=temp1->next;
else
temp2=temp2->next;
}
//calling traverse function to traverse the list
cout<<"resultantList :\t";
traverse();
}
int main()
{
//inserting elements in list1
push1(1);
push1(2);
push1(3);
push1(4);
push1(6);
cout<<"List 1 :\t";
traverse1();
cout<<endl<<endl;
//inserting elements in list2
push2(2);
push2(4);
push2(6);
push2(8);
cout<<"List 2 :\t";
traverse2();
cout<<endl<<endl;
//calling the function findIntersect to find the intersection
findIntersection(head1,head2);
return 0;
}
No comments:
Post a Comment