The Gateway to Computer Science Excellence
+2 votes
99 views
How to trace the below program?

p and q are the starting address of two different linked list

 

 

struct node*Do(struct node*p,struct node*q){
    struct node*ps,*qs;
    if(!p){
        return(q);
    }
    else if(!q){
        return(p);
    }
    else{
        ps=p->link;
        qs=q->link;
        p->link=q;
        q->link=Do(ps,qs);
        return(p);
    }
}
in Programming by (57 points) | 99 views

2 Answers

+2 votes

If list 1 is a-->b-->c and list 2 is d-->e then output will be a-->d-->b-->e-->c


Procedure:

by Loyal (5.6k points)
edited by
0 votes

take 2 different list with same length and blindly apply the code
1) a-b-c-d
2) g-h-i-j

take 2 different list with lengths are not same and blindly apply the code
1) a-b-c-d-e-f
2) g-h-i-j

take 2 different list with lengths are not same and blindly apply the code
1) a-b-c-d
2) g-h-i-j-k-l

 

Answer:: They are merging those lists, where stats with p, take a node from p list and take a node from q list and repeat until one of list completed, after that remaining list is keep it as it is

a-g-b-h-c-i-d-j-e-f is the o/p for the i/p

1) a-b-c-d-e-f
2) g-h-i-j
by Veteran (64.7k points)
0
I want to know that how the function calls push and pop in the stack for the above recursion.

can you give me the stack diagram of the function calls?
0

 Shaik Masthan  can you please check the output once more because i am getting something different

0

Rishav Kumar Singh,

May be i mistaken... but i didn't found it,i still getting my answer is true, Please post your output, i will compare

0

this is what i am getting

0
i am not getting your solution...

is it a-d-b-e-c ?
0
NO, can you post you solution please
+1
i hope you mistakenly forget that, p is a local variable
+1
sorry ,  Shaik Masthan sir you are right. Now i got it

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,492 answers
195,439 comments
100,696 users