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;
}
Your loeschen will never works becouse you pass to it just a NULL. You first need to correctly construct the list. First, you need to pass your Kopf (a kind of reference to it) to the readSteine function, and assign it to each new next, and after that assign to it the new domino, and so on to form the linked list..
I strongly recommend you to read Programming: Principles and Practice Using C++ .
You mix C functions and style of programming with C++. You dont even need to know these C functions which are hard to learn right in order to begin to learn C++.