This is one of the programming questions asked during written test from Microsoft. I am giving the question and the answer that I came up with. Thing is my answer although l
//I have used recursions .
//Sorry for such a long code.
//:) it works,hope it helps.
#include
#include
#include
struct node{
int data;
struct node *next ;
};
struct node *start1=NULL,*start2=NULL;
struct node*start=NULL;
struct node *create_ll1(struct node *start);
struct node *create_ll2(struct node *start);
void sorted_ll(struct node* node1,struct node* node2);
struct node *display(struct node *start);
void p(struct node*);
main(){
start1=create_ll1(start1);
start2=create_ll2(start2);
//start1=display(start1);
printf("\n");
//start2=display(start2);
sorted_ll(start1,start2);
//start=display(start);
}
struct node *create_ll1(struct node *start1){
struct node *ptr,*new_node;
int num;
printf("Enter -1 for ending \n");
printf("Enter data for list 1: \n");
scanf("%d",&num);
while(num!=-1){
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
if(start1==NULL){
new_node->next=NULL ;
start1=new_node;
}
else{
ptr=start1 ;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=new_node;
new_node->next=NULL ;
}
printf("Enter data: \n");
scanf("%d",&num);
}
return start1;
}
struct node *create_ll2(struct node *start2){
struct node *ptr,*new_node;
int num;
printf("Enter -1 for ending \n");
printf("Enter data for list 2: \n");
scanf("%d",&num);
while(num!=-1){
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
if(start2==NULL){
new_node->next=NULL ;
start2=new_node;
}
else{
ptr=start2 ;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=new_node;
new_node->next=NULL ;
}
printf("Enter data: \n");
scanf("%d",&num);
}
return start2;
}
struct node *display(struct node *start){
struct node *ptr;
ptr=start;
while(ptr->next!=NULL){
printf("\t %d",ptr->data);
ptr=ptr->next;
}
printf("\t %d",ptr->data);
printf("\n");
return start ;
}
void sorted_ll(struct node* node1,struct node* node2)
{
if(!node1){
p(node2);
exit(0);
}
else if(!node2){
p(node1);
exit(0);
}
if(node1->datadata){
printf("%d\t",node1->data);
sorted_ll(node1->next,node2);
}
else{
printf("%d\t",node2->data);
sorted_ll(node1,node2->next);
}
}
void p(struct node* pme){
while(pme->next!=NULL){
printf("%d \t",pme->data);
pme=pme->next;
}
printf("%d",pme->data);
}