简体   繁体   English

结构,链表,标头,空指针,C ++

[英]Struct, linked list, head,null-pointer, C++

How can I define,that the pointer head shows the first linked list element?The compiler shows mistake in the place : 我如何定义指针头显示第一个链接列表元素?编译器在以下位置显示错误:

head=domino->info; 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. 首先,由于左侧和右侧的类型不兼容,因此无法执行分配head=domino->info

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==1) ...替换为

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

The final version of the readDomino() function might look like this: readDomino()函数的最终版本可能如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM