Delete Node Error

Hi there..
I'm having much trouble in my following program Delete function is not working properly my rest program is working fine (that's what I think only)
Thanks in advance to helping hands
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
using namespace std;

struct Node
{
	char name[15];
	int ID;
	int age;
	float height;
	Node *next;
};

	Node *temp;
	Node *head;
	Node *tail;

	bool IsEmpty();
	bool isFull();
	void AddNode ();
	void DeleteNode ();
	void DeleteList();
	void Search();
	void Display ();
	bool isEmpty()
	{
		return (head==0 && tail==0);
	}

	bool isFull()
	{
		return false;
	}


	void AddNode ()
	{
		temp=new Node;
		cout<<"Enter the ID of person: ";
		cin>>temp->ID;
		cout<<"Enter the name of person: ";
		cin>>temp->name;
		/*cout<<"Enter the age of person: ";
		cin>>temp->age;
		cout<<"Enter the height of person: ";
		cin>>temp->height;*/
		temp->next=NULL;

		if (head==0 && tail==0)
		{
			head= temp;
			tail= temp;
		}
		else
		{
			tail->next=temp;
			tail= temp;
		}

	}

	void Search()
	{
		int Search_ID;
		cout<<"Enter the ID to search: ";
		cin>>Search_ID;
		bool flag=true;
		
		temp=head;
		while (temp != NULL)
		{
			if (temp->ID == Search_ID)
			{
				cout<<"ID of person: "<<temp->ID<<endl;
				cout<<"Name of person: "<<temp->name<<endl;
			}
			else
			{
				flag=false;
			}
			
			temp=temp->next;
		}
			if (flag==false)
			{
				cout<<"No record found with that ID\n";
			}
	}

	void Delete ()
	{
		int delete_ID;
		cout<<"Enter the ID to delete: ";
		cin>>delete_ID;

		Node *prev=head;
	
		if (head == tail && delete_ID== head->ID)
		{
			delete head;
			head=tail=NULL;
		}
		else if (delete_ID=head->ID)
		{
			
			temp= head;
			head=head->next;
			delete temp;
		}
		else
		{
			temp=prev->next;
			while (temp != NULL)
			{
				if (temp->ID == delete_ID)
				{
					prev=temp->next;
					delete temp;
				}
				temp=temp->next;
				temp=prev->next;
			}
		}

	}

	void Display()
	{
		temp=head;
		cout<<"\n\nList is: \n";
		while (temp != NULL)
		{
			cout<<"Name of person: "<<temp->name<<endl;
			temp=temp->next;
		}
	}
	int main ()
	{
		for (int k=0; k<5; k++)
		{
			AddNode();
		}
		Search();
		Delete();
		Display();
	}
Your Search() function is wrong too. You're setting flag=false if any node in the list doesn't match. I think you'll find that if one node mathes and one node doesn't then it prints the matching node followed by "no record found." To fix this, break out of the loop when you find the matching record. Then after the loop, just check if temp==NULL. If it does then you didnt' find the record.

Oh, use nullptr instead of NULL.

As for delete, I suggest you consider the following cases separately:
- the list is empty.
- You delete the first item in the list
- You delete the nth item in the list

In the last 2 cases, also consider what happens if you delete the last item in the list. Write your code to handle these cases in this order.
Topic archived. No new replies allowed.