I have a structure LastTimeOn that has next pointer to the same structure. I don't want to introduce the back pointer. I am struggling with clearing this structure, can u help me? The program runs in endless loop REM2REM3REM4REM2REM3REM4 etc:
class Pump {
public:
LastTimeOn *lastTimeOn;
Pump();
};
class LastTimeOn{
public:
unsigned int dayOfWeek;
unsigned int hour;
unsigned int minute;
LastTimeOn(unsigned int minute1, unsigned int hour1, unsigned int dayOfWeek1);
LastTimeOn *next;
};
void clearLastTimeOn(Pump *pump1) {
Serial.print("REM1");
while (pump1->lastTimeOn != NULL) {
LastTimeOn *lastTimeOn = pump1->lastTimeOn;
Serial.print("REM2");
while (lastTimeOn->next != NULL) {
Serial.print("REM3");
lastTimeOn = lastTimeOn->next;
}
Serial.print("REM4");
delete lastTimeOn;
if (pump1->lastTimeOn->next == NULL){
Serial.print("REM4a");
delete pump1->lastTimeOn;
pump1->lastTimeOn = NULL;
}
}
Serial.print("REM5");
}
I create new data like this:
lastTimeOn->next = new LastTimeOn(minute, hour, dayOfWeek);