I'm working on single linked lists in which I got a problem with add function which adds integers into sorted way. But my program keeps crashing. I've been working all the night but I couldn't find the problem. Does anyone has any idea about this?
Thank you
template<typename T>
class SLList
{
private:
struct Node
{
Node(const T& val= T(), Node* next_ptr=NULL) : data(val), next(next_ptr) { }
// data members
T data;
Node *next;
};
template<typename T>
void SLList<T>::add(const T& val)
{
if (find(val)==false)
{
Node *curr= head;
Node* prev=NULL;
if(head->next==NULL)
{
cout<<"head";
Node *tmp=new Node(val,head->next);
head->next=tmp;
return;
}
else
{
while(curr->data < val && curr!=NULL)
{
curr=curr->next;
prev=curr;
cout<<"add";
}
Node *tmp=new Node(val, prev->next);
//head->next=tmp;
}
}
} `
nullPointer2nullPointer2
asked May 6, 2012 at 14:54
-
If you've been working all night, I'm sure you don't mind telling us where it crashes, or showing the code for Node.Luchian Grigore– Luchian Grigore2012年05月06日 14:56:44 +00:00Commented May 6, 2012 at 14:56
-
struct Node { // constructor Node(const T& val= T(), Node* next_ptr=NULL) : data(val), next(next_ptr) { } // data members T data; Node *next; }; public: SLList(); SLList(const SLList & lst2); ~SLList(); const SLList& operator=(const SLList & rhs); int size() const; void print() const; bool find(const T& val) const; void add(const T& val); void remove(const T& val); private: Node *head; };nullPointer2– nullPointer22012年05月06日 14:58:35 +00:00Commented May 6, 2012 at 14:58
-
Node is the copy constructor and it crashes in while loop.nullPointer2– nullPointer22012年05月06日 14:59:08 +00:00Commented May 6, 2012 at 14:59
-
Please add the code to the question (click on edit).Luchian Grigore– Luchian Grigore2012年05月06日 14:59:17 +00:00Commented May 6, 2012 at 14:59
-
I added the code. Could you please check?nullPointer2– nullPointer22012年05月06日 15:38:00 +00:00Commented May 6, 2012 at 15:38
2 Answers 2
The while
exit condition is inverted:
while(curr->data < val && curr!=NULL)
should be
while( curr!=NULL && curr->data < val )
If curr
is NULL
, it will crash (well, UB to be exact) before it checks for NULL
.
answered May 6, 2012 at 14:59
5 Comments
nullPointer2
It still crashing in the same while loop.
nullPointer2
I have no idea but its going to while loop
Luchian Grigore
@user1141641 use ideone.com to post code that compiles and exibits the problem. Otherwise, there's no way to tell with the amount of information you're providing. If you want help, you have to put some effort in it as well.
Luchian Grigore
@user1141641 just go to ideone.com, paste your code, choose C++, and then submit.
nullPointer2
how do we add another application.cpp file for input in ideone?
Also, move prev = curr
forward:
while(curr != NULL && curr->data < val) {
prev = curr;
curr = curr->next;
cout << "add";
}
answered May 6, 2012 at 15:13
2 Comments
nullPointer2
It's only adding the first value. It's not adding the remaining values
Igor F.
You are missing
prev->next = tmp;
or similar after the while loop.lang-cpp