01-07-2008, 11:14
|
|
|
|
חבר מתאריך: 08.10.02
הודעות: 827
|
|
אצלי הקוד לא קורס - VC6. חוץ מזה אני רואה פה הרבה new בלי delete אחד בקוד מה שאומר שבטח יש פה דליפות. חוץ מזה יש פה עוד כמה בעיות, כמו אי שימוש בפרמטרים ועוד כמה שורות שלא הבנתי למה עשית, סידרתי את זה ככה מהר ותראה מה יצא:
קוד:
struct Projectile
{
//...members...
Projectile *next;
unsigned int ID;
};
void projectileAdd(Projectile *start, Projectile *node)
{
if (start == NULL) return; //No starting point
while(start->next != NULL) start = start->next; //Travel the list to find the head
start->next = node; //Add a new one
}
void projectileRemove(Projectile *start, Projectile *node)
{
if (start == NULL || node == NULL || start == node) return; //No root or Nothing to find or Tring to remove the root
for (; start != NULL; start = start->next) {
if (start->next == node) {
start->next = node->next; //Skip this one
return;
}
}
}
int main(int argc, char* argv[])
{
//Create root node
Projectile *root = new Projectile;
root->next = NULL;
root->ID = 0;
//Add a 10 node to the root
for (unsigned int i = 0; i < 10; i++) {
Projectile *p = new Projectile;
p->next = NULL;
p->ID = i+1;
projectileAdd(root, p);
}
//Print the list
printf("\n*** Printing list:\n");
for (Projectile *currente = root; currente != NULL; currente = currente->next) {
printf("ID: %d\n", currente->ID);
}
//Add 3 nodes
Projectile *a = new Projectile;
a->next = NULL;
a->ID = 11;
projectileAdd(root, a);
Projectile *b = new Projectile;
b->next = NULL;
b->ID = 12;
projectileAdd(root, b);
Projectile *c = new Projectile;
c->next = NULL;
c->ID = 13;
projectileAdd(root, c);
//Remove node whose ID is 12 and 11
projectileRemove(root, b); delete b;
projectileRemove(root, a); delete a;
//Remove ID 7
for (Projectile *currenta = root; currenta != NULL; currenta = currenta->next) {
if (currenta->ID == 7) {
projectileRemove(root, currenta);
delete currenta;
break;
}
}
//Print the list
printf("\n*** Printing list:\n");
for (Projectile *current = root; current != NULL; current = current->next) {
printf("ID: %d\n", current->ID);
}
//Remove all
printf("\n*** Removing all:\n");
for (Projectile *currentb = root->next, *temp; currentb != NULL; currentb = temp) {
printf("Removing ID: %d\n", currentb->ID);
projectileRemove(root, currentb);
//Save the next node address and delete the current one
temp = currentb->next;
delete currentb;
}
//Print the list
printf("\n*** Printing list:\n");
for (Projectile *currentd = root; currentd != NULL; currentd = currentd->next) {
printf("ID: %d\n", currentd->ID);
}
//Delete the root
delete root;
root = NULL;
return 0;
}
_____________________________________
Error: Keyboard not found
Press F1 to continue
|