How to clear the filled list in C ++? That created an empty class. I start to fill it with a certain function, and now I need to reset it? Will you help with the function? Here is the class itself:

#include <iostream> #include <conio.h> #include <string.h> using namespace std; struct element { string data; element *adress; }; class List { private: element * StartAdress, *FinishAdress; public: List() { StartAdress = NULL; FinishAdress = NULL; } void ElAdd(string a) { element *e; e = new element; e->data = a; if (StartAdress == NULL) { StartAdress = e; } else { FinishAdress->adress = e; } FinishAdress = e; FinishAdress->adress = NULL; } void print() { element *e; for (e = StartAdress; e != NULL; e = e->adress) cout << e->data << " " << endl; } }; 

I myself thought that:

 void ElDel (string a) { StartAdress == NULL; FinishAdress == NULL; } 

But it seems not worked. Tell me?

  • Right. Garbage behind a need to clean up. - alexlz
  • Those. all delete one by one? And all at once can not be done? - navi1893
  • Not. You yourself write the function that clears the list. - Only here void ElDel conceived by you (string a) is something else. Apparently this is a function in some sense inverse to void ElAdd (string a). And it should remove from the list the element (or all elements?) Whose data is a. Something like this ? - avp
  • @avp yes, I'm sorry. The name of the function did not work out. I just have to clear the entire list! So here I thought that if you assign NULL to the initial and final value, then I reset it. But something seems wrong - navi1893
  • So you did not assign, and compared to equality. But you just can't assign 0 anyway. - IronVbif

1 answer 1

Enumerate all objects starting with StartAdress. We memorize the address of the StartAdress object into a temporary variable, move the StartAdress pointer to the next element and make a delete for the temporary variable. And so on until the list ends.