Hello,

I want to write a function which takes two linked list and check smaller

The function CheckSmaller should return 1 if num1 points to a linked list which represents a smaller number than the number pointed to by num2 linked list. Otherwise, it returns -1. If both linked list point to exactly the same number, CheckSmaller returns a 0.

#include<iostream>
using namespace std;

class Node
{
public:
	int value;
	Node * next;
	Node();
	Node(int);
};
	Node::Node():value(0),next(NULL){}
	Node::Node(int v):value(v),next(NULL){}
	int checkSmallersmaller(Node *num1, Node *num2)
int sum (Node *num, int s) 
{
if (num==0)
{
	return s;
}
return sum (num->next, s*10+num->data);
}

int checkSmallersmaller(Node *num1, Node *num2)
{
int s1 = sum (num1, 0);
int s2 = sum (num2, 0);
if (s1< s2)
{
return 1;
}
else if (s1==s2)
{
	return 0;
}
return -1;
}

Edited 5 Years Ago by BigDeveloper: n/a

I suggest you make a small compilable example. What is the problem? Compiler errors? Crash? Incorrect result?

Line 21 should be:

return sum (num->next, s*10+num->value); //value, not data.

Other than that, I think it should work and do what it is supposed to do. Please give specific errors that the code produces.

Hey, make sure that you add ; at the end of everything. like where it says null on the 11th line there isnt any ; or after that. those little mistakes make the program not work.

I'm getting this error

fatal error LNK1169: one or more multiply defined symbols found

#include<iostream>
using namespace std;

class Node
{
public:
	int value;
	Node * next;
	Node();
	Node(int);
};
	Node::Node():value(0),next(NULL){}
	Node::Node(int v):value(v),next(NULL){}
	int checkSmallersmaller(Node *num1, Node *num2);
int sum (Node *num, int s) 
{
if (num==0)
{
	return s;
}
return sum (num->next, s*10+num->value);
}

int checkSmallersmaller(Node *num1, Node *num2)
{
int s1 = sum (num1, 0);
int s2 = sum (num2, 0);
if (s1< s2)
{
return 1;
}
else if (s1==s2)
{
	return 0;
}
return -1;
}

Edited 5 Years Ago by BigDeveloper: n/a

This article has been dead for over six months. Start a new discussion instead.