Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<iostream>
#include<stack>
using namespace std;
void recursion(stack s1,stack s2)
    stack<int>s1;
     stack<int>s2;
struct node
{int data;
node *next;};
typedef struct node*lptr;
 
 
 
 
 
 int main(){
 
    stack<int>s1;
     stack<int>s2;
    
     
     
 int mp;
 cin>>mp;
lptr l1;
int n,m;

while(n!=-1)
cin>>n;
s1.push(n);
l1=new(node);
l1->data=n;
l1->next=NULL;

lptr l2;

while(m!=-1)
cin>>m;
s2.push(m);
l2=new(node);
l2->data=m;
l2->next=NULL;
cout<<recursion(s1,s2);
return 0;}
void="" recursion(stack="" s1,stack="" s2)
="" {if="" (s1.pop()="=s2.pop())
" recursion(s1,s2);
="" cout<<s1.pop();
="" }

<b="">What I have tried:

pls check this errors not able to point out my mistake
Posted
Updated 9-May-22 5:13am
Comments
CPallini 9-May-22 9:30am    
Could you please detail your scenario?
Sai Vishal 9-May-22 9:38am    
yeah sure !!
ICC - Meets in Links : Create two Linked lists with the given input numbers terminated by -1

L1 : 8 5 2 4 6 9 3 1 7 -1

L2 : 36 18 72 15 -1

Test case

Input :

9 ( meeting point of L1 and L2)

8 5 2 4 6 9 3 1 7 -1

36 18 72 15 -1

output : ( meeting point, values in the meet )

9

9 3 1 7
CPallini 9-May-22 10:04am    
Still not clear to me. For instance, how is defined the 'meet point'?
Sai Vishal 9-May-22 10:13am    
yeah thats the point here ! input is to be given for meeting point and the same have to be printed
i had an image of question explaining it briefly kindly mail me if u wanna look at it
CPallini 9-May-22 10:22am    
Couldn't you provide a convincing description?

Quote:
pls check this errors not able to point out my mistake

You haven't told us what the problem might be, and I can't run the code at all in it's current format, or with the same inputs as you used even if I could. And that's essential to fixing code problems!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Quote:
pls check this errors not able to point out my mistake

First mistake is incorrect indentation of code:

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include<iostream>

#include<stack>

using namespace std;
void recursion(stack s1, stack s2)
stack < int > s1;
stack < int > s2;
struct node {
    int data;
    node * next;
};
typedef struct node * lptr;

int main() {

    stack < int > s1;
    stack < int > s2;

    int mp;
    cin >> mp;
    lptr l1;
    int n, m;

    while (n != -1)
        cin >> n;
    s1.push(n);
    l1 = new(node);
    l1 -> data = n;
    l1 -> next = NULL;

    lptr l2;

    while (m != -1)
        cin >> m;
    s2.push(m);
    l2 = new(node);
    l2 -> data = m;
    l2 -> next = NULL;
    cout << recursion(s1, s2);
    return 0;
}
void recursion(stack s1, stack s2) {
    if (s1.pop() = s2.pop())
        recursion(s1, s2);
    cout << s1.pop();
}

Indentation style - Wikipedia[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
-----
Then I suspect some missing "{" cand "}" in your code:
replace
C++
while (n != -1)
    cin >> n;
s1.push(n);
l1 = new(node);
l1 -> data = n;
l1 -> next = NULL;

lptr l2;

while (m != -1)
    cin >> m;
s2.push(m);
l2 = new(node);
l2 -> data = m;
l2 -> next = NULL;

with
C++
while (n != -1) {
    cin >> n;
    s1.push(n);
    l1 = new(node);
    l1 -> data = n;
    l1 -> next = NULL;
}
lptr l2;

while (m != -1) {
    cin >> m;
    s2.push(m);
    l2 = new(node);
    l2 -> data = m;
    l2 -> next = NULL;
}

There is other weird things in your code, but since you did not explain how your code go wrong, it is difficult to say.
A good programming question is:
- source code
- Error message or wrong behavior description.
- Sample input, expected output, actual output
 
Share this answer
 
Comments
Graeme_Grant 9-May-22 11:22am    
You forgot naming of variable names to make code readable, ie: self describing...
Patrice T 9-May-22 11:29am    
Not forgot anything, only highlighting a problem hidden by wrong indentation.
And didn't spoke about using uninitialized m and n, among other problems.
Graeme_Grant 9-May-22 12:36pm    
Only poking fun ... yep, there are issues with that code...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900