943,614 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 419
  • C++ RSS
Mar 5th, 2009
0

linked list prob

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
azwraith69 is offline Offline
21 posts
since Jul 2008
Mar 5th, 2009
0

Re: linked list prob

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.
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 5th, 2009
0

Re: linked list prob

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
azwraith69 is offline Offline
21 posts
since Jul 2008
Mar 5th, 2009
0

Re: linked list prob

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.

C++ Syntax (Toggle Plain Text)
  1. while (i!=NULL && j!=NULL)
  2. {
  3. //do stuff
  4. i=i->GetNext();
  5. j=j->GetNext();
  6. }
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007
Mar 5th, 2009
0

Re: linked list prob

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?
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: trying to insert a new node into a linkedlist
Next Thread in C++ Forum Timeline: N-ary Tree Destructor





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC