How can I delete all objects from a linked list,implemented in struct? After my delete-funktion (loeschen) are shown comic numbers and I don´t know any more where my pointers are.
struct domino {
int data1;
int data2;
domino *next;
};
int readSteine (){ //Reading from numbers from file
FILE *file;
if((file=fopen("datei.dat", "r") )==NULL) { /
std::cout<<"File cant be open"<<std::endl;
return 0;
}else {
int beginning;
int temp;
fscanf(file, "%d", &beginning);
for(int i=0; i<beginning; i++) {
domino *domino1= new domino;
//if(i>0) temp2->next=domino1;
fscanf(file, "%i", &temp);
domino1->data1=temp;
fscanf(file, "%i", &temp);
domino1->data2=temp;
printf("[%d:%d]", domino1->data1, domino1->data2);
domino1->next=0;
}
}return 0;
}
Function for deleting of the list:
void deletelist (domino *head) {
domino* tmp;
while(head != 0) {
tmp=head->next;
delete head;
head=tmp;
}
}
int main() {
domino *pHead=NULL;
domino s;
readSteine();
deletelist(pHead);
std::cout<<s.data1<<"...."<<s.data2<<std::endl;
return 0;
}
Below is an example of a clear method. The idea is to traverse over the list and delete the nodes one by one.