Hello,

Can anyone make sure the two functions are correct?

First, this is function should return true if all of the nodes are positive.otherwise, false.

bool AllPositive(Node * h);
{
node* current= head;
while (current !=NULL)
{
if (current < 0)
{
	return false;
}
else
{
	return true;
}

Second,this function takes takes the heads of the 2 linked lists and returns true if h1 has more nodes than h2.

for this function I think it is something that has to do with the length. so I wrote this but im not sure where to start.

int Length( node* h) 
{
node* current = head;
int count = 0;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}

Yeah they both seem fine ,.... however may i suggest that for trivial things like this try thinking up some simple test programs ,... I dont want to sound like a know it all but im starting to take the agile approach to coding in which you plan how to test your code before you write the code ,.... this ensures that you write tests to test what you want it to do and you dont get sneaky in the tests to hide border cases.

But yeah to answer just your question they look fine .

The first function is riddled with errors:

1) It is missing two right curly braces.

2) Its parameter is named h but the body of the function uses an otherwise undefined variable named head.

3) The loop goes through only one iteration.

4) The code compares the pointer named current with 0, rather than comparing the contents of the node with 0.

5) The parameter is a Node* rather than a const Node*, even though there is no need for the function to modify the contents of any node.

The second function shares errors (2) and (5) with the first function. In addition, there is a subtle algorithmic disadvantage to what I think is the approach you're trying to follow, which is to compute the length of each list and then compare the lengths. The trouble with this approach is that if one of the lists is much longer than the other -- say, one has 10 elements and the other has 1,000,000 elements, your program will march out to the ends of both lists even though it really needed only to determine that the longer list had only a single element.

One way to solve this problem is to have two pointers that you step together each time through a single loop. As soon as either of them reaches the end of the list, you have your answer; it depends only on which pointer got to the end first (or whether they did so at the same time).

The loop would therefore look something like this:

while (p != NULL && q != NULL) {
    p = p -> next;
    q = q -> next;
}

@ aerkoenig ,... actually yeah your right .... i just kinda looked at them and went ,..it looks about right ,.. but now you mentiontion it but yeah your totally right ,..sorry

Thank you for helping me out
appreciate it :D

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.