Saturday, 15 June 2013

c++ - Linked List not looping properly -



c++ - Linked List not looping properly -

here code:

void setupeachflechette(int numflechettes){ int = 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 "<<totalnum<<endl<<endl; char yes = null; cout<<"ready? "; cin>>yes; = 0; next->link = null; next = head; while(next != null){ next->display(); next = next->link; i++; }

}

for reason, when looping through linked list, displaying first 4 nodes in list , continues repeat first four. cannot end on null can run through while(next != null) loop. wondering how come coding doesn't loop through of flechettes? reference, should loop through 20 different flechettes, not 4 flechettes 'i' number of times.

i think functions pretty self explanatory. if arent' allow me know , i'll explain them you.

thank of help

p.s. learning pointers , linked lists please bare me.

you not modifying variable totalnum before printing. think code should this

void setupeachflechette(int numflechettes){ int = 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 "<<totalnum<<endl<<endl; char yes; cout<<"ready? "; cin>>yes; = 0; next->link = null; next = head; while(next != null){ next->display(); next = next->link; i++; } }

in original code head node lastly node , head->next null

i expect initializing fellow member variable link null within constructor of flechette

c++ list

No comments:

Post a Comment