Struct, linked list, head,null-pointer, C++

806 views Asked by At

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;
    }

}
1

There are 1 answers

1
Dima Chubarov On

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

  struct dominoone* tail=NULL;

then after you have populated the structure replace your if (i==1) ... with

  if (i == 0 ) head = domino;
  else tail->next = domino;
  tail = domino;

The final version of the readDomino() function might look like this:

int readDomino (){
        FILE *datei;
        struct dominoone* tail=NULL;


        if((datei=fopen("datei.dat", "r") )==NULL) {
            std::cout<<"File can't be opened"<<std::endl;
            return -1;
        }

        int list_size;
        fscanf(datei, "%d", &list_size);

        for(int i=0; i<list_size; i++) {
                dominoone *domino= new dominoone;
                fscanf(datei, "%i", &domino->info.number1);
                fscanf(datei, "%i", &domino->info.number2);
                printf("[%d:%d]\n",
                       domino->info.number1, 
                       domino->info.number2);
                domino->next=NULL;

                if (i == 0 ) head = domino;
                else tail->next = domino;
                tail = domino;
           }
        }
        return 0;
}