<分区>
这是我的代码:
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 个节点,并继续重复前四个节点。我也无法让它以 null 正确结束,所以我可以通过 while(next != null) 循环运行它。我想知道为什么我的编码没有遍历所有的 Flechettes?作为引用,它应该循环遍历 20 个不同的飞弹,而不仅仅是循环“i”次 4 个飞弹。
我认为这些功能是不言自明的。如果他们不告诉我,我会向您解释。