Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct nodo {
  5. int chiave;
  6. struct nodo *prec, *succ;
  7. };
  8. typedef struct nodo nodo;
  9.  
  10. struct paridispari {
  11. nodo *pari;
  12. nodo *dispari;
  13. };
  14. typedef struct paridispari paridispari;
  15.  
  16. paridispari PariDispari(nodo *a);
  17.  
  18. int main() {
  19. nodo *first, *second, *third, *fourth, *fifth, *sixth;
  20. first = (nodo *) malloc(sizeof(nodo));
  21. second = (nodo *) malloc(sizeof(nodo));
  22. third = (nodo *) malloc(sizeof(nodo));
  23. fourth = (nodo *) malloc(sizeof(nodo));
  24. fifth = (nodo *) malloc(sizeof(nodo));
  25. sixth = (nodo *) malloc(sizeof(nodo));
  26.  
  27. first->chiave = 1;
  28. first->prec = sixth;
  29. first->succ = second;
  30.  
  31. second->chiave = 2;
  32. second->prec = first;
  33. second->succ = third;
  34.  
  35. third->chiave = 3;
  36. third->prec = second;
  37. third->succ = fourth;
  38.  
  39. fourth->chiave = 4;
  40. fourth->prec = third;
  41. fourth->succ = fifth;
  42.  
  43. fifth->chiave = 5;
  44. fifth->prec = fourth;
  45. fifth->succ = sixth;
  46.  
  47. sixth->chiave = 6;
  48. sixth->prec = fifth;
  49. sixth->succ = first;
  50.  
  51. paridispari result;
  52.  
  53. result = PariDispari(first);
  54.  
  55. printf("%d\n", result.pari->chiave);
  56. printf("%d\n", result.dispari->chiave);
  57. }
  58.  
  59. paridispari PariDispari(nodo *a) {
  60. paridispari result = {NULL, NULL};
  61.  
  62. if (a == NULL) {
  63. result.pari = NULL;
  64. result.dispari = NULL;
  65. return result;
  66. }
  67.  
  68. nodo *head_pari, *head_dispari, *current, *next_pari, *next_dispari;
  69.  
  70. nodo *main_head;
  71. int i = 0;
  72.  
  73. while (a != main_head) {
  74.  
  75. if (a->chiave % 2 == 0) {
  76. if (result.pari == NULL) {
  77. a->prec = NULL;
  78. result.pari = a;
  79. } else {
  80. result.pari->succ = a;
  81. }
  82. } else {
  83. if (result.dispari == NULL) {
  84. a->prec = NULL;
  85. result.dispari = a;
  86. } else {
  87. result.dispari->succ = a;
  88. }
  89. }
  90.  
  91. if (i == 0) {
  92. main_head = a;
  93. i++;
  94. }
  95. a = a->succ;
  96. }
  97.  
  98. return result;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement