[problem] Traversing through list & delete matching name

I am trying to traverse through a linked list by name and then delete the matching name. I'm having some problems understanding why it isn't working like I want it to.
If I am typing in names: fred, roy, ben, rory.
It will be layed out like this: rory - ben - roy - fred - NULL

Now, I'm attempting to remove fred, and then trying to display the list again. It's only displaying "roy". And if I remove only "roy" - it will remove "roy" and "rory", and only display ( ben , fred ).
My explanation is probably just confusing you more. I need to understand why this is happening and how I can solve 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
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
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

struct Database
{
	string name;
	int age;
	Database* p_next_element;
};

Database* DeleteUser (Database* p_people);

void ShowList (Database* p_people)
{

	system("cls");
	while (p_people != NULL)
	{
		cout << "Name: " << p_people->name << endl;
		cout << "Age: " << p_people->age << "\n\n";
		p_people = p_people->p_next_element;
	}
}

Database* getNewPerson (Database* p_people)
{
	system("cls");
	Database* p_new = new Database;
	cout << "name of new person: ";
	cin >> p_new->name;
	cout << "age of " << p_new->name << ' ';
	cin >> p_new->age;
	p_new->p_next_element = p_people;
	return p_new;
}

int main()
{
	Database* p_people = NULL;
	int choice = 0;
	bool Quit = false;
	while ( Quit == false )
	{
		system("cls");
		cout << "1. new person" << endl;
		cout << "2. view list" << endl;
		cout << "3. delete person" << endl;
		cout << "0. Exit" << endl;
		cout << "choice: ";
		cin >> choice;
		
		switch (choice)
		{
		case 1:
			p_people = getNewPerson (p_people);
			break;
		case 2:
			ShowList (p_people);
			cin.get();
			cin.get();
			break;
		case 3:
			p_people = DeleteUser (p_people);
			cin.get();
			cin.get();
			break;
		case 0:
			Quit = true;
			break;
		default:
			cout << "Wrong value";
			cin.get();
			cin.get();
			break;
		}
		
	}
}



Database* DeleteUser (Database* p_people)
{
	system("cls");
	string temp = "";
	cout << "Name of the user you want to delete? ";
	cin >> temp;

	Database* p_current = p_people;

	if( p_current == NULL ) return NULL;
	if( p_current->name == temp )
	{
		p_current = p_people->p_next_element;
		delete p_people;
		return p_current;
	}

	while (p_current->p_next_element != NULL)
	{
			if(p_current->p_next_element->name == temp)
			{
				Database* p_temp = p_current->p_next_element;
				p_current->p_next_element = p_temp->p_next_element;
				delete p_temp;
				cout << "Sucessfully deleted user " << temp << endl;
				break;
			}
			else
				p_current = p_current->p_next_element;
	}
	return p_current;
}
You're returning p_current from the delete function back to main to be the new p_people value. p_current might be in the middle of the list if you had to traverse through a few nodes to find the name you want to delete.

So when you go to view the list after deleting, p_people might be pointing to a node in the middle of the list. Or - if you go to add a new person, the new node will be added in the middle and you'll lose access to the original earlier part of the list.
I see. But how can I set it back to point to the starting point again (beginning of list).
I'd at least maintain a pointer that always points to the first node to the list. It will need to be updated when a new node is added or when the first node is removed. Then you can always use this pointer to find the front of the list.

Sometimes it's referred to as the head or root. There are a couple of examples here:

http://www.cprogramming.com/tutorial/lesson15.html
http://www.cplusplus.com/articles/LACRko23/
Thanks a lot, thats where I made a mistake. I followed your advice and fixed it.
Topic archived. No new replies allowed.