Linked List

Apr 21, 2012 at 2:29pm
Given a number x, write a program to delete all occurrences of x from a linked list
in one iteration. That is, you can not scan the list more than once.

It only deletes the first occurrence of X, can someone help fixing it please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void DelAcc(Node*& head, int X)
{
	if((!head)||(!head->next && head->value!=X))
	{
		cout<<"could not find "<<X<<" in the list\n";
		return;
	}
	if(!head->next && head->value==X) {delete head; head=NULL; return;}
	Node* tmp = head;
	Node* t;
	while(tmp->next)
	{
		if(tmp->next->value == X)
		{
			t = tmp->next;
			tmp->next = tmp->next->next;
			delete t;
			return;
		}
		tmp=tmp->next;
	}
	cout<<"could not find "<<X<<" in the list\n";
}
Apr 21, 2012 at 2:41pm
Apr 21, 2012 at 3:27pm
noway!! O.o

and why do not you help me out, so i don't have to spit it out more than once u pry!!!
Apr 21, 2012 at 3:31pm
I already told you how to fix it. Even so, you should try by yourself.
That you still have the return in there shows that you have no idea what it actually does. The obvious thing to do is to look that up. Why haven't you done that long ago?
Apr 21, 2012 at 4:38pm
O.o I and removed it and run the code, and still i am facing the same problem!!! nothing been changed.
Last edited on Apr 21, 2012 at 4:39pm
Apr 21, 2012 at 4:41pm
I explained that as well. Step through your code line by line and reproduce on paper what it does when feeding your test list to the function. Then you'll see why it's wrong.
Apr 21, 2012 at 4:42pm
instead of piffling, why do not you run the code on your compiler rather than giving instruction by looking!! and tell me what you gonna get?!
Apr 21, 2012 at 4:45pm
I don't need to run it to see that it's wrong and why it's wrong.
But you should already know that yourself, after all you don't get the desired results.

Here's two relevant lists you can use for your pen and paper experiment: 1 2 2 3
And for good measure: 1 2 2 3 2
Last edited on Apr 21, 2012 at 4:48pm
Apr 21, 2012 at 5:01pm
i already submit it, thanks anyways, and sorry buddy if i offended you or to anyone in any way.

//still it is not working properly
Topic archived. No new replies allowed.