linked list prob

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 21
Reputation: azwraith69 is an unknown quantity at this point 
Solved Threads: 0
azwraith69's Avatar
azwraith69 azwraith69 is offline Offline
Newbie Poster

linked list prob

 
0
  #1
Mar 5th, 2009
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)
    {
		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);
					}
				}
			}
		}
		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...
I am living in a mere program...
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: linked list prob

 
0
  #2
Mar 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 21
Reputation: azwraith69 is an unknown quantity at this point 
Solved Threads: 0
azwraith69's Avatar
azwraith69 azwraith69 is offline Offline
Newbie Poster

Re: linked list prob

 
0
  #3
Mar 5th, 2009
Originally Posted by kbshibukumar View Post
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!
I am living in a mere program...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: linked list prob

 
0
  #4
Mar 5th, 2009
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.

  1. while (i!=NULL && j!=NULL)
  2. {
  3. //do stuff
  4. i=i->GetNext();
  5. j=j->GetNext();
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: linked list prob

 
0
  #5
Mar 5th, 2009
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC