0

これが私のコードです:

void setUpEachFlechette(int numFlechettes){

int i = 0;
int totalNum = 0;

Flechette* next;
Flechette* head;
Flechette* pEnd;
Flechette* temp;

    while(numFlechettes != i){

        double x = getRandomNumberX();
        double y = getRandomNumberX();
        double z = getRandomNumberZ();


         if(i != 0)
          temp = next;

         next = new Flechette;

         next->setXYZ(x, y, z);

         if(i == 0)
            head = next;
         else
          next->link = temp;

         i++;

         next->display();

    }


 cout<<"\nThe total number of flechettes is "<<totalNum<<endl<<endl;

 char yes = NULL;

 cout<<"Ready? ";
 cin>>yes;

 i = 0;

 next->link = NULL;
 next = head;
 while(next != NULL){

    next->display();
    next = next->link;

    i++;

 }

}

何らかの理由で、リンクされたリストをループすると、リストの最初の 4 つのノードのみが表示され、最初の 4 つのノードが繰り返されます。また、null で適切に終了させることもできないため、while(next != null) ループを実行できます。私のコーディングがすべてのフレシェットをループしないのはなぜですか? 参考までに、4 つのフレシェット 'i' 回だけでなく、20 の異なるフレシェットをループする必要があります。

関数はかなり自明だと思います。それらが私に知らせなければ、私はあなたにそれらを説明します.

4

2 に答える 2

1

totalNum印刷する前に変数を変更していません。また、コードはこのようにする必要があると思います

void setUpEachFlechette(int numFlechettes){

int i = 0;
int totalNum = 0;

Flechette* next;
Flechette* head;
Flechette* pEnd;
Flechette* temp;
srand (time(NULL));
    while(numFlechettes != i){

        int x = rand();
        int y = rand();
        int z = rand();


         if(i != 0)
          temp = next;

         next = new Flechette;

         next->setXYZ(x, y, z);

         if(i == 0)
            head = next;
         else
          temp->link = next;

         i++;

         next->display();

    }

totalNum = numFlechettes;
 cout<<"\nThe total number of flechettes is "<<totalNum<<endl<<endl;

 char yes;

 cout<<"Ready? ";
 cin>>yes;

 i = 0;

 next->link = NULL;
 next = head;
 while(next != NULL){

    next->display();
    next = next->link;

    i++;

 }
}

元のコードheadでは、ノードが最後のノードになりhead->nextNULL

のコンストラクター内でメンバー変数linkを適切に初期化していることを期待しますNULLFlechette

于 2013-02-21T04:40:42.080 に答える
0

単純な単一リンク リストを処理するには、2 つの方法があります。1 つは常にリストの先頭に追加することで、これが最も簡単な方法です。

struct Node
{
    Node* next;

    Node()
        : next(nullptr)  // Make sure the `next` pointer is not pointing anywhere
        {}
};

Node* head = nullptr;

while (add_one_more_node())
{
    Node* newNode = new Node;

    // Make the `next` point to the old head
    newNode->next = head;

    // Make the head point to the new node
    head = newNode;
}

2 番目の方法は、リストの最後のノードも追跡し、最後に挿入することです。これは少しトリッキーです:

// Assume `Node` structure as in above example

Node* head = nullptr;
Node* tail = nullptr;

while (add_one_more_node())
{
    Node* newNode = new Node;

    if (tail == nullptr)
    {
        // List is empty
        head = tail = newNode;
    }
    else
    {
        // List is not empty

        // Make the current tails next link point to the new node
        tail->next = newNode;

        // Make the new node the next tail
        tail = newNode;
    }
}

どちらの方法でも、同じループを使用してリストを反復処理できます。

// Loop over the list
for (Node* node = head; node != nullptr; node = node->next)
{
    // ...
}

リストを解放するには、もう少し複雑なループが必要なので、次のポインターを取得する前にノードを解放しません。

for (Node* node = head, *next; node != nullptr; node = next)
{
    // Next node to iterate to
    next = node->next;

    // Free the current node
    delete node;
}
于 2013-02-21T04:54:37.103 に答える