- #1
mathmari
Gold Member
MHB
- 5,049
- 7
Hey!
I have to write a function for the destruction of the planetary system "solid".
With the destruction of the planetary system, the asteroids where the gap between this one and the object, is smaller that "gap" will also be destructed. (so, they have to be deleted) The asteroids for which the gap is greater are converted to free-floating planets so they have to be inserted into the tree of free-floating planets. The total cost of the transport of all the asteroids that are converted into free-floating planets in the tree of free-floating planets should be $O(n)$, where $n$ is the maximal between the elements of the tree of free-floating planets of the star system to which the object belongs and the elements of the tree of asteroids of the planetary system of which the object has identifier "solid". The field "gap" of the new tree should have the value $0$. The destructed planetary system should be deleted from the list of planetary systems of the star system to which it belonged.
(The tree of free-floating planets is not a binary search tree)
I have done the following:
Is it correct?? (Wondering)
Is the way I deleted the nodes correct?? (Wondering)
I have to write a function for the destruction of the planetary system "solid".
With the destruction of the planetary system, the asteroids where the gap between this one and the object, is smaller that "gap" will also be destructed. (so, they have to be deleted) The asteroids for which the gap is greater are converted to free-floating planets so they have to be inserted into the tree of free-floating planets. The total cost of the transport of all the asteroids that are converted into free-floating planets in the tree of free-floating planets should be $O(n)$, where $n$ is the maximal between the elements of the tree of free-floating planets of the star system to which the object belongs and the elements of the tree of asteroids of the planetary system of which the object has identifier "solid". The field "gap" of the new tree should have the value $0$. The destructed planetary system should be deleted from the list of planetary systems of the star system to which it belonged.
(The tree of free-floating planets is not a binary search tree)
I have done the following:
Code:
int destruction(int solid, int gap){
int ffplanpos=-1;
int i, j=-1;
int sum=0;
asteroid_t *planf = calloc(1, sizeof(asteroid_t));
asteroid_t *f=NULL;
plansys_t *p=NULL;
asteroid_t *next=NULL;
starsy_t *fflp=NULL;
starsy_t *prev_fflp=NULL;
for(i=0; i<Sfreep; i++){
p=StarS[i].plasy;
while (p != NULL && p->solid != solid){
p=p->next;
}
if(p != NULL && p->solid == solid){
j=i;
}
}
if(j == -1){
return -1;
}
p=StarS[j].plasy;
if(p == NULL){
printf("The planetary system with identifier %d couldn't be found\n", solid);
return -1;
}
if(p->asteroids == NULL){
printf("The planetary system doesn't contain any asteroids\n");
return -1;
}
f=p->asteroids;
while(f != NULL){
if(gap<f->gap){
next=f->RC;
free(f);
f = next;
}
else{
next=f->LC;
free(f);
f = next;
}
}
p->asteroids = f;
if (f != NULL) {
f->PARENT=NULL;
}
fflp=StarS[j].ff;
if(f != NULL){
planf->as=f->as;
planf->gap=0;
planf->RC=NULL;
planf->LC=NULL;
planf->PARENT=NULL;
if(fflp == NULL){
fflp = planf;
}
else{
while(fflp->RC != NULL){
prev_fflp=fflp;
fflp=fflp->RC;
}
prev_fflp->RC=planf;
}
}
return 0;
}
Is it correct?? (Wondering)
Is the way I deleted the nodes correct?? (Wondering)