In the below code snippet written in C++ node is defined to create nodes of Linked-List . The function insert1 is written to create an integer linked-list and while creating the list we have used counters in the main() (function) to identify the number of nodes in each linked-list . Similarly, we have created another function insert2 to create another linked-list of character data-type. Accordingly we have to merge the two lists alternatively . This is achieved by the function merge_alternate_new . Taking one element from integer list and other element from character list.
First of all we have checked the number of nodes of both the Linked-Lists and this function works for the lists having equal number of nodes .
The two display functions are created two display the two initial lists and then display the final list.
Input:-- 1->2->3->4 Input:-- a->s->d->f Output:-- 1->a->2->s->3->d->4->f
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct node
{
int di;
char dc;
node *link;
}*head1=NULL , *head2=NULL, *ptr2 , *ptr1, *nptr,*prev,*fptr2,*fptr1;
void insert1()
{
int item;
cout<<"enter Integer value :"<<endl;
cin>>item;
if(head1==NULL)
{
nptr=new node;
nptr->di=item;
nptr->link=NULL;
head1=nptr;
}
else
{
ptr1=head1;
nptr=new node;
while(ptr1!=NULL)
{
if(ptr1->link==NULL)
{
nptr->di=item;
ptr1->link=nptr;
nptr->link=NULL;
}
ptr1=ptr1->link;
}
}
}
void insert2()
{
char item;
cout<<"Enter Character Value :"<<endl;
cin>>item;
if(head2==NULL)
{
nptr=new node;
nptr->dc=item;
nptr->link=NULL;
head2=nptr;
}
else
{
ptr2=head2;
nptr=new node;
while(ptr2!=NULL)
{
if(ptr2->link==NULL)
{
nptr->dc=item;
ptr2->link=nptr;
nptr->link=NULL;
}
ptr2=ptr2->link;
}
}
}
void display2()
{
ptr2=head2;
cout<<endl<<"entered character list is :\n"<<endl;
while(ptr2!=NULL)
{
cout<<ptr2->dc<<"-->";
ptr2=ptr2->link;
}
cout<<"NULL"<<endl;
}
void display1()
{
ptr1=head1;
cout<<endl<<"entered Integer list is :\n"<<endl;
while(ptr1!=NULL)
{
cout<<ptr1->di<<"-->";
ptr1=ptr1->link;
}
cout<<"NULL"<<endl;
}
void merge_alternate_new(int c1, int c2) /* merging alternate elements of two linked list: */
{
cout<<"After merging both lists:\n";
ptr2=head2;
ptr1=head1;
if(c1==c2)
{
while(ptr1!=NULL)
{
cout<<ptr1->di<<"-->";
cout<<ptr2->dc<<"-->";
ptr1=ptr1->link;
ptr2=ptr2->link;
}
cout<<"NULL"<<endl;
}
else
cout<<"\nBoth list should contain same number of elements \n";
}
int main()
{
int choice,count=0,count1=0;
while(1)
{
cout<<"ENTER YOUR CHOICE";
cout<<" \n1. INSERTION IN FiRST LIST ";
cout<<"\n2. INSERTION IN SECOND LIST ";
cout<<"\n3. FOR DISPLAY LIST1 ";
cout<<"\n4. DISPLAY LIST2 ";
cout<<"\n5. MERGE BOTH LIST ";
cout<<"\n6. EXIT\n "<<endl;
cin>>choice;
switch(choice)
{
case 1:
insert1();
count++;
break;
case 2:
insert2();
count1++;
break;
case 3:
display1();
break;
case 4:
display2();
break;
case 5:
merge_alternate_new(count,count1);
break;
case 6:
exit(0);
break;
default: cout<<"\n enter a valid choice ";
}
}
getch();
return 0;
}
Output:
No comments:
Post a Comment