Advertisement
meraxes

Union of sets using linked lists #2

Nov 11th, 2017
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct node *position;
  6.  
  7. typedef struct node
  8. {
  9. int num;
  10. position next;
  11. }_NODE;
  12.  
  13. void ReadFromFile(position);
  14. void Print(position);
  15. void Union(position un, position p, position q);
  16. int main(int argc, char *argv[])
  17. {
  18.  
  19. _NODE L1,L2,PRS,UN;
  20.  
  21. L1.next=NULL;
  22. L2.next=NULL;
  23. PRS.next = NULL;
  24.  
  25. ReadFromFile(&L1);
  26. Print(L1.next);
  27.  
  28. ReadFromFile(&L2);
  29. Print(L2.next);
  30.  
  31. printf("\n");
  32.  
  33. Union(&UN, L1.next, L2.next);
  34. Print(UN.next);
  35.  
  36. return 0;
  37. }
  38. void ReadFromFile(position p)
  39. {
  40. FILE *input;
  41. char file[50];
  42. position tmp,z;
  43.  
  44. printf("\nFile name:");
  45. scanf(" %s", &file);
  46.  
  47. input = fopen(file, "r");
  48.  
  49. if(input == NULL)
  50. {
  51. printf("File not opened.");
  52. }
  53. else
  54. {
  55. while(!feof(input))
  56. {
  57. z=(position) malloc(sizeof(_NODE));
  58. if(z==NULL)
  59. printf("Greska u alokaciji memorije.");
  60. else
  61. {
  62. fscanf(input," %d", &z->num);
  63.  
  64. tmp=p;
  65.  
  66. while(tmp->next != NULL && tmp->next->num < z->num)
  67. tmp = tmp->next;
  68.  
  69. z->next = tmp->next;
  70. tmp->next = z;
  71. }
  72.  
  73. }
  74. fclose(input);
  75. }
  76.  
  77. }
  78. void Print(position p)
  79. {
  80. while (p != NULL)
  81. {
  82. printf("%d\t",p->num);
  83. p=p->next;
  84. }
  85. }
  86.  
  87.  
  88. void Union(position un, position p, position q)
  89. {
  90. position z,tmp;
  91.  
  92. while (p != NULL && q != NULL)
  93. {
  94.  
  95. z = (position)malloc(sizeof(_NODE));
  96. if (z == NULL)
  97. {
  98. printf("Memory allocation error!");
  99. break;
  100. }
  101. else
  102. {
  103. if (p->num < q->num)
  104. {
  105. z->num = p->num;
  106. z->next = un->next;
  107. un->next = z;
  108.  
  109. p = p->next;
  110. }
  111. else if (p->num > q->num)
  112. {
  113. z->num = q->num;
  114. z->next = un->next;
  115. un->next = z;
  116.  
  117. q = q->next;
  118. }
  119. else
  120. {
  121. z->num = p->num;
  122.  
  123. z->next = un->next;
  124. un->next = z;
  125.  
  126. p = p->next;
  127. q = q->next;
  128. }
  129. if (z->num == un->num)
  130. {
  131. un->next = z->next;
  132. free(z);
  133. }
  134. }
  135. }
  136. if (p == NULL)
  137. {
  138. tmp = q;
  139. while (tmp != NULL)
  140. {
  141. z = (position)malloc(sizeof(_NODE));
  142. if (z == NULL)
  143. {
  144. printf("Memory allocation error!.");
  145. break;
  146. }
  147. else
  148. {
  149. z->num = tmp->num;
  150. z->next = un->next;
  151. un->next = z;
  152. tmp = tmp->next;
  153. }
  154. }
  155. }
  156. if (q == NULL)
  157. {
  158. tmp = p;
  159. while (tmp != NULL)
  160. {
  161. z = (position)malloc(sizeof(_NODE));
  162. if (z == NULL)
  163. {
  164. printf("Memory allocation error!");
  165. break;
  166. }
  167. else
  168. {
  169. z->num = tmp->num;
  170. z->next = un->next;
  171. un->next = z;
  172. }
  173. tmp = tmp->next;
  174. }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement