从文件读入链表

时间:2017-03-21 12:17:04

标签: c++ linked-list

任何人都可以帮我这个代码吗?我试图将文件中的数据读入链表。我的链表是这样的:

文件中的数据格式如下:

 ####****###

**####*****#

等等

           node *copy = new node();
           rownum=1;
           seatnum=1;

           while(!fin.eof())
           {
            fin>>copy->data;
            cout<<copy->data;
            copy->next=NULL;

            if(head==NULL)
            {
                copy->next=head;
                head=copy;
            }
            cout<<"Row number-" <<copy->row<<"    "<<copy->data;
            copy->row=rownum;
            copy->seat=seatnum;
            seatnum=seatnum+1;
            if (seatnum==total)// total is total number of seats in a row
            {
                seatnum=1;
                rownum=rownum+1;
            }


    }

1 个答案:

答案 0 :(得分:0)

在每次循环中你必须创建一个新的链表

node *copy = new node();
       rownum=1;
       seatnum=1;

       while(!fin.eof())
       {
         node *tmp= new node();
        fin>>tmp->data;
        cout<<tmp->data;
        tmp->next=NULL;

        if(head==NULL)
        {
            tmp->next=head;
            head=tmp;
        }
        cout<<"Row number-" <<tmp->row<<"    "<<copy->data;
        tmp->row=rownum;
        tmp->seat=seatnum;
        copy->next = tmp;
        seatnum=seatnum+1;
        if (seatnum==total)// total is total number of seats in a row
        {
            seatnum=1;
            rownum=rownum+1;
        }


}