Linked List Delete Node

When I delete first node it work fine..
But when I want to delete specific node its not working fine :(

for example
# Value
1 12
2 22
3 32
4 42
5 52

here i want to delete Node 3
But outPut come like this...
# Value
1 12
2 22
3 32

Here I only want to delete Node # 3

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
  void deleteNode(int num)
{
	int pos=1;
	node *temp, *pre;
	temp=start;			// assigning start node address to temp
	if(temp==NULL)   // Checking list is empty?
	{
		cout<<"*************List Is empty************";
		return;
	}
	if(num==pos)					// If user want to delte first node 
	{
		if(temp->next==NULL)	// checking first node is last?
		{
			start=NULL;		// List Will Become Empty
			cout<<"There Was Only One Node ... Now List Is Empty";
			return;
		}
		start=temp->next;
		return;
	}
	while (temp->next!=NULL)
		{
			pre=temp;
			temp=temp->next;
			pos++;
			if(pos==num)
			{
				if(temp->next=NULL)
				{
					pre->next=NULL;
					cout<<"You Deleted Last Node Of Your List..."<<endl;
					return;
				}
				else
				{
					pre->next=temp->next;
					return;
				}
			}
		}
}
Last edited on
Try this.
You cross over temp in line 37 pre->next=temp->next but you don't delete temp from the list.

Your code does not tell whether this is C or C++m
C:
1
2
pre->next=temp->next;
free(temp);


C++
1
2
pre->next=temp->next;
delete temp;
thank you so much for your precious time :)

I'm using C++
Here I am not deleting the node but removing its address from list. I will use the delete function after this, still I am understanding linking of nodes.
Can we see your print function
Here It is..... node *start... is public...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void display()
{
	node *temp;
	int num=1;
	temp=start;
	if(temp==NULL)
	{
		cout<<"Link List Is Empty...."<<endl;
		return;
	}
	cout<<"Node #"<<setw(10)<<"Value"<<endl;
	while(temp!=NULL)
	{
		cout<<setw(2)<<num<<setw(11)<<temp->value<<endl;
		temp=temp->next;
		num++;
	}
	cout<<endl;
}
Last edited on
Still Have Problem
Topic archived. No new replies allowed.