Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. struct starsy {
  2. int ss; /* The star system identifier. >0 */
  3. plansys_t *plasy; /* Pointer to the first element in the list of the planetary system */
  4. plansys_t *Sentinel; /* Pointer to the sentinel node of the list of the planetary system */
  5. asteroid_t *ff; /* The free-floating planets tree */
  6. };
  7.  
  8.  
  9. struct plansys {
  10. int solid; /* The planetary system identifier. >0 */
  11. asteroid_t *asteroids; /* Pointer to the first node in the list of the asteroids */
  12. asteroid_t *asentinel; /* Pointer to the sentinel node of the asteroids tree */
  13. plansys_t *next; /* Pointer to the next node in the list of the planetary systemt */
  14. };
  15.  
  16.  
  17. struct asteroid {
  18. int as; /* The asteroid identifier. >0 */
  19. int gap; /* The absolute gap from the object of the planetary system */
  20. asteroid_t *PARENT; /* Pointer to the parent node in the asteroids tree */
  21. asteroid_t *LC; /* Pointer to the left child in the asteroids tree */
  22. asteroid_t *RC; /* Pointer to the right child in the asteroids tree */
  23. };
  24.  
  25. starsy_t StarS[N]; /*The array of the star systems, it is an array of lists */
  26.  
  27. int Sfreep; /*An index to the first free position in the array of the star systems*/
  28.  
  29.  
  30. asteroid_t *Insert(plansys_t *p, asteroid_t *new) {
  31. if (p->asteroids == p->asentinel) p->asteroids=new;
  32. if (new->gap < p->asteroids->gap) {
  33. if (p->asteroids->LC == p->asentinel){
  34. p->asteroids->LC = new;
  35. new->LC=p->asentinel;
  36. new->RC=p->asentinel;
  37. }
  38. else
  39. Insert(p->asteroids->LC, new);
  40. }
  41.  
  42. if (new->gap > p->asteroids->gap) {
  43. if (p->asteroids->RC == p->asentinel){
  44. p->asteroids->RC = new;
  45. new->LC=p->asentinel;
  46. new->RC=p->asentinel;
  47. }
  48. else
  49. Insert(p->asteroids->RC, new);
  50. }
  51. return p->asteroids;
  52. }
  53.  
  54.  
  55.  
  56.  
  57. void In_Order(plansys_t *p) {
  58. if (p->asteroids == p->asentinel) return;
  59. In_Order(p->asteroids->LC);
  60. printf(" %d , %d n", p->asteroids->as, p->asteroids->gap);
  61. In_Order(p->asteroids->RC);
  62. }
  63.  
  64.  
  65.  
  66. int asteroid_constitution(int as, int gap, int solid) {
  67. int i, g=0, k=-1;
  68.  
  69. plansys_t *p=NULL;
  70. asteroid_t *plasy_sent=NULL;
  71.  
  72. for(i=0; i<Sfreep; i++){
  73. p=StarS[i].plasy;
  74. while (p != StarS[i].Sentinel && p->solid != solid){
  75. p=p->next;
  76. }
  77. if(p != StarS[i].Sentinel && p->solid == solid){
  78. k=i;
  79. }
  80. }
  81. if(k == -1){
  82. return -1;
  83. }
  84.  
  85. asteroid_t *ast = calloc(1, sizeof(asteroid_t));
  86.  
  87. ast->as=as;
  88. ast->gap=gap;
  89. ast->PARENT=NULL;
  90. ast->LC=NULL;
  91. ast->RC=NULL;
  92.  
  93. p=StarS[k].plasy;
  94. plasy_sent=p->asentinel;
  95.  
  96. p->asteroids=Insert(p, ast);
  97. In_Order(p);
  98.  
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement