Qua ho provato ad usare la funzione per eliminare la prima occorrenza ma non permette alla print di stampare nulla, non capisco il motivo...
#include <iostream>
#include <string>
struct cell{
std::string str;
cell *next;
};
typedef cell *list;
void DeleteFirstOccurence(list& l)
{
cell *cur;
cell *cur1;
cell *prev;
for (cur = l; cur != nullptr; cur = cur->next)
{
prev = cur;
for (cur1 = l; cur1 != nullptr && cur1->str != cur->str; cur1 = cur1->next){}
if (cur1 == l) l = l->next;
else
prev->next = cur->next;
delete cur;
}
}
void InserisciInTesta(list& l, std::string s)
{
cell *aux = new cell;
aux->str = s;
aux->next = l;
l = aux;
}
void print(list l)
{
cell *aux = l;
while (aux != nullptr)
{
std::cout << aux->str << ' ';
aux = aux->next;
}
std::cout << std::endl;
}
int main()
{
list l = nullptr;
InserisciInTesta(l, "mela");
InserisciInTesta(l, "mela");
InserisciInTesta(l, "mela");
InserisciInTesta(l, "pera");
InserisciInTesta(l, "pera");
InserisciInTesta(l, "arancia");
DeleteFirstOccurence(l);
print(l);
}