As if I said nothing guys I eventually managed to do it and now it works as I expected... this is my code:
Code:
#include <iostream>
#include <string>
using namespace std;
struct Group
{
int age;
string name;
Group* next_element;
};
Group* GetNewElement (int years, string name, Group* head)
{
Group* person = new Group;
person->age = years;
person->name = name;
person ->next_element = head;
return person;
}
void List (Group* person)
{
cout<<person->name<<" ";
cout<<person->age<<" years old"<<endl;
}
void Deleting (string value, Group* head)
{
Group* p_current = NULL;
Group* p_prev = NULL;
for (p_current = head; p_current != NULL; p_prev = p_current, p_current = p_current->next_element)
{
if (p_current->name == value)
{
if (p_prev==NULL)
head=p_current->next_element;
else
p_prev->next_element = p_current->next_element;
}
}
}
int main ()
{
int years;
string name;
int input;
Group* person;
Group* head = NULL;
string Delete;
while (input != 1)
{
cout<< "Enter a person's name"<<endl;
cin>>name;
cout<< "Enter his/her age "<<endl;
cin>>years;
person, head = GetNewElement (years, name, head);
cout << " Press 1 if you want to show the list of the people set so far, otherwise press any other number to keep adding people"<<endl;
cin>>input;
}
Group* p_current = head;
Group* p_prev = NULL;
while (p_current != NULL)
{
List(p_current);
p_current=p_current->next_element;
}
cout<<"Select the one you want to delete from the list by his name"<<endl;
cin >> Delete;
Deleting (Delete, head);
cout<<"This is your final list: "<<endl;
p_current=head;
while (p_current != NULL)
{
List(p_current);
p_current=p_current->next_element;
}
}
Could you only suggest me where I should deallocate all the memory I ve used?