hello,,:)

i have problem doing a FOR loop in my linked list,, it doesn't start the loop,, i don't know why...

class node
{
public:
    node(int number, node*nxt):number(number),nxt(nxt){}
    int getNumber(){return number;}
    void setNumber(int number){this->number=number;}
    node* getNxt(){return nxt;}
    void setNxt(node *){this->nxt=nxt;}
private:
    int number;
    node *nxt;
};

bool mergeList(node *&result, node *list1, node *list2)
{
    if(!result)
    {
		[B]for(node *i=list1, *j=list2; i->getNxt()!=NULL || j->getNxt()!=NULL;\
			i=i->getNxt(), j=j->getNxt())
		{
			if(!result)
			{
				if(i->getNumber() <= j->getNumber())
				{
					result = i;
					result->setNxt(j);
					result = j;
					result->setNxt(NULL);
				}
				else
				{
					result = j;
					result->setNxt(i);
					result = i;
					result->setNxt(NULL);
				}
			}
			else
			{
				if(i->getNumber() <= j->getNumber())
				{
					if(i->getNumber() <= result->getNumber())
						return false;
					else
					{
						result->setNxt(i);
						result = i;
						result->setNxt(j);
						result = j;
						result->setNxt(NULL);
					}
				}
				else
				{
					if(j->getNumber() <= result->getNumber())
						return false;
					else
					{
						result->setNxt(j);
						result = j;
						result->setNxt(i);
						result = i;
						result->setNxt(NULL);
					}
				}
			}
		}[/B]
		return true;
    }
    else
        return false;
}

the loop doesn't run,, and i'm sure RESULT is empty when i passed it (function always return TRUE)...

thx...

Recommended Answers

All 4 Replies

Is the value of result != NULL when you invoke the function? I don't see you do a new operation on result anywhere in the function.

As you stated, if it is really non-null, the program would crash because you are accessing the member functions of result in a number of places.

Is the value of result != NULL when you invoke the function? I don't see you do a new operation on result anywhere in the function.

As you stated, if it is really non-null, the program would crash because you are accessing the member functions of result in a number of places.

thx.. but i think you misunderstood some things,, i need the RESULT to be empty.. not "non-null"... if it is non-null, then false..

do i need a new? i already assigned i(a node) to result..
correct me if im wrong..

what i dont understand is why my FOR loop doesn't run..

thx!

For loops are designed mainly for integer incrementation. They support other datatypes, but this will often result in confusing problems like this. Instead, use a while loop.

while (i!=NULL && j!=NULL)
{
//do stuff
i=i->GetNext();
j=j->GetNext();
}

OK.. I understood.
However, I think your sequence would enter the for loop only if both list1->getNxt() == NULL and list2->getNxt() == NULL because of the terminationg condition in for loop. Are you meaning the same logic?

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.