0

漠然としているように見えるかもしれませんが、本当に申し訳ありません。私はファイルに書き込み、この単一リンクリスト内のソートされたノードをコンソールに出力しています。残念ながら、ソートリストでは、先頭に余分な0を出力して書き込み、最後に値を切り取ります。コードは次のとおりです。

void SLLIntStorage::Read(istream& r)
{
    char c[13];
    r >> c;
    r >> numberOfInts;

    head = new Node;
    head->next = NULL;
    tail = head;
    r >> head->data;

    for (int i = 0; i < numberOfInts; i++)
    {
        Node* newNode = new Node;
        r >> newNode->data;

        if(_sortRead)
        {
            if(newNode->data > tail->data)
            {
                tail->next = newNode;
                tail = newNode;
            }
            else if(head->data > newNode->data)
            {
                newNode->next = head;
                head = newNode;
            }
            else
            {
                current = head;

                while(current->next != NULL)
                {
                    if(current->next->data > newNode->data)
                    {
                        newNode->next = current->next;
                        current->next = newNode;
                        break;
                    }
                    else
                    {
                        current = current->next;
                    }
                }
            }
        }
        else
        {
            tail->next = newNode;
            tail = newNode;
        }
    }
    print();
}

void SLLIntStorage::Write(ostream& w)
{
    current = head;

    for(int i = 0; i < numberOfInts; i++)
    {
        w << current->data << endl;

        if (current->next != NULL)
            current = current->next;
    }
}
void SLLIntStorage::print()
{
    current = head;

    for(int i = 0; i < numberOfInts; i++)
    {
        cout << current->data << endl;
        //system("pause");
        if(current->next != NULL)
        {
            current = current->next;
        }
    }
}

ファイルサンプル: 0 0 1 2 2 3 ........ 9995 9996 99969998 //ここでは別の9998と想定

4

1 に答える 1

0

1つのエントリを読みすぎたようです。最初に、 -loopr >> head->data; の直前の行のエントリを読み取ります。次に、-loop内の追加のエントリforを読み取り、合計エントリを取得します。numberOfIntsfornumberOfInts+1

于 2011-05-02T13:58:26.897 に答える