| | |
linked list prob
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
hello,,
i have problem doing a FOR loop in my linked list,, it doesn't start the loop,, i don't know why...
the loop doesn't run,, and i'm sure RESULT is empty when i passed it (function always return TRUE)...
thx...
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;
}thx...
I am living in a mere program...
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
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.
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.
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...
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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)
while (i!=NULL && j!=NULL) { //do stuff i=i->GetNext(); j=j->GetNext(); }
![]() |
Similar Threads
- subset sum (C++)
- pls help. link list prob (C)
- choose a random element from a sequence (C++)
- Linked Lists Help C++ (C++)
- selction sort linked list (C++)
- printing a linked list (C++)
Other Threads in the C++ Forum
- Previous Thread: trying to insert a new node into a linkedlist
- Next Thread: N-ary Tree Destructor
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





