0

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;
 }
}
} `
asked May 6, 2012 at 14:54
5
  • 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. Commented 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; }; Commented May 6, 2012 at 14:58
  • Node is the copy constructor and it crashes in while loop. Commented May 6, 2012 at 14:59
  • Please add the code to the question (click on edit). Commented May 6, 2012 at 14:59
  • I added the code. Could you please check? Commented May 6, 2012 at 15:38

2 Answers 2

1

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

It still crashing in the same while loop.
I have no idea but its going to while loop
@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.
@user1141641 just go to ideone.com, paste your code, choose C++, and then submit.
how do we add another application.cpp file for input in ideone?
0

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

It's only adding the first value. It's not adding the remaining values
You are missing prev->next = tmp; or similar after the while loop.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.