How can I define,that the pointer head shows the first linked list element?The compiler shows mistake in the place :
head=domino->info;
struct dominopart {
int number1;
int number2;
};
struct dominoone {
dominopart info;
struct dominoone *next;
};
First I defined this as null-pointer,because the linked list is empty at first.
struct dominoone* next= NULL;
struct dominoone* head=NULL;
Then the information from the file is read line by line.
int readDomino (){
FILE *datei;
if((datei=fopen("datei.dat", "r") )==NULL) {
std::cout<<"File can't be opened"<<std::endl;
return 0;
}else {
int beginning;
int temp;
fscanf(datei, "%d", &beginning);
for(int i=0; i<beginning; i++) {
dominoone *domino= new dominoone;
fscanf(datei, "%i", &temp);
domino->info.number1=temp;
fscanf(datei, "%i", &temp);
domino->info.number2=temp;
printf("[%d:%d]", domino->info.number1, domino->info.number2);
domino->next=0;
if(i==1) {
head=domino->info; //The compiler shows mistake here
}
}
}return 0;
}
The file is read and shows this,that I have written in the file correctly,but I can't delete the whole list,because my pointer head is still null-pointer:
void del () {
dominoone* tmp;
while(head != 0) {
tmp=(head)->next;
delete head;
head=tmp;
}
}
There are few errors in this code.
First the assignment
head=domino->info
cannot be performed since the types of the left and right sides are incompatible.Second, on reading the list of dominos, you need to keep track of the tail of the list.
So you need to declare
then after you have populated the structure replace your
if (i==1) ...
withThe final version of the
readDomino()
function might look like this: