Wednesday 26 September 2012

MERGING TWO LINKED LIST WITH PRINT EVEN LOCATION OF LINKED LIST

#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *link;
};
void main()
{
int i,n,d;
struct node *ptr1,*temp,*ptr,*start=NULL,*start1=NULL;
temp=new node;
temp->data=10;
temp->link=NULL;
start=temp;
cout<<"How many node do u want in 1st lined list:-";
cin>>n;
cout<<"Enter data in first linked list"<<endl;
for(i=1;i<=n;i++)
{
cin>>d;
 temp=new node;
temp->data=d;
temp->link=start;
start=temp;
}

cout<<"data is:-"<<endl;
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<" "<<temp->data<<" ";
}
//-----------------------------------------------

temp=new node;
temp->data=10;
temp->link=NULL;
start1=temp;
cout<<endl<<"How many node do u want in 2nd linked list:-";
cin>>n;
cout<<"Enter data in first linked list"<<endl;
for(i=1;i<=n;i++)
{
cin>>d;
 temp=new node;
temp->data=d;
temp->link=start1;
start1=temp;
}

cout<<"data is:-"<<endl;
for(temp=start1;temp!=NULL;temp=temp->link)
{
cout<<" "<<temp->data<<" ";
}


cout<<endl<<"After merging of two linked list"<<endl;
        ptr1=start;
while(ptr1->link!=NULL)
{
ptr1=ptr1->link;
}
ptr1->link=start1;
ptr1=start;
while(ptr1!=NULL)
{
cout<<" "<<ptr1->data<<" ";
ptr1=ptr1->link;
}


cout<<endl<<"data print at even node "<<endl;


temp=start;
while(temp!=NULL)
{
for(i=1;i<=2;i++)
{
ptr=temp;
temp=temp->link;
}
cout<<" "<<ptr->data<<" ";
}

getch();
}

No comments:

Post a Comment