Singly Linked List Delete Function Error

Feb 19, 2013 at 6:53pm
Hii guyz..
There's a problem with this code, it just crashes.. this function is supposed to get a word from the user as a parameter and delete every node that contains it..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void LinkDel(char * r)
		{
			int flg;
			flg=0;
            Node *temp, *m;
            temp=Root;
            while(temp!=NULL)
			{
                 if(stricmp(temp->getString(),r)==0)
				 {
                      if(temp==Root)
					  {
                        Root=temp->getNext();
                        delete temp;
						flg++;
                        continue;
					  }
                       else
					   {
                        m->setNext(temp->getNext());
						flg++;
                        delete temp;
						
                        continue;
					   }
				 }
				 else
				 {
                       m=temp;
                       temp= temp->getNext();
				 }

			}

			if (flg>0)
			{
				cout <<r<<" is removed"<<endl;
			}
			else
				cout <<r<<" not found"<<endl;


}//end of LinkDel 
Feb 19, 2013 at 7:41pm
This code snip

1
2
3
4
5
6
7
8
9
10
11
            while(temp!=NULL)
			{
                 if(stricmp(temp->getString(),r)==0)
				 {
                      if(temp==Root)
					  {
                        Root=temp->getNext();
                        delete temp;
						flg++;
                        continue;
					  }


is already invalid because temp was deleted and at the same time it is tested in the condition of the loop and used in stricmp
Last edited on Feb 19, 2013 at 7:42pm
Feb 19, 2013 at 7:43pm
Learn to indent.

You've got
1
2
delete temp;
continue;
so `temp' is pointing to garbage.

continue will continue the loop.
To break, use break
Feb 19, 2013 at 8:02pm
-Yeah.. I noticed that.. nd tried to break but same problem occurs :s-
Tried another break-point.. It seems it was sending trash as a parameter
Thanks for trying to help :)
Last edited on Feb 19, 2013 at 8:09pm
Topic archived. No new replies allowed.