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;
}

Recommended Answers

All 8 Replies

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.

Indeed.

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;
}

It builds fine for me after I add main:

int main()
{
}

thnx for helping me out ,,, I really appreciate it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.