Advertisement
meraxes

Union of sets using linked lists #2

Nov 11th, 2017
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 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 Cross(position, position, position);
  16. void Union(position un, position p, position q);
  17. int main(int argc, char *argv[])
  18. {
  19.  
  20. _NODE L1,L2,PRS,UN;
  21.  
  22. L1.next=NULL;
  23. L2.next=NULL;
  24. PRS.next = NULL;
  25.  
  26. ReadFromFile(&L1);
  27. Print(L1.next);
  28.  
  29. ReadFromFile(&L2);
  30. Print(L2.next);
  31.  
  32. printf("\n");
  33.  
  34. /*Cross(&PRS, L1.next, L2.next);
  35. Print(PRS.next);*/
  36.  
  37. printf("\n");
  38.  
  39. Union(&UN, L1.next, L2.next);
  40. Print(UN.next);
  41.  
  42. return 0;
  43. }
  44. void ReadFromFile(position p)
  45. {
  46. FILE *input;
  47. char file[50];
  48. position tmp,z;
  49.  
  50. printf("\nFile name:");
  51. scanf(" %s", &file);
  52.  
  53. input = fopen(file, "r");
  54.  
  55. if(input == NULL)
  56. {
  57. printf("File not opened.");
  58. }
  59. else
  60. {
  61. while(!feof(input))
  62. {
  63. z=(position) malloc(sizeof(_NODE));
  64. if(z==NULL)
  65. printf("Greska u alokaciji memorije.");
  66. else
  67. {
  68. fscanf(input," %d", &z->num);
  69.  
  70. tmp=p;
  71.  
  72. while(tmp->next != NULL && tmp->next->num < z->num)
  73. tmp = tmp->next;
  74.  
  75. z->next = tmp->next;
  76. tmp->next = z;
  77. }
  78.  
  79. }
  80. fclose(input);
  81. }
  82.  
  83. }
  84. void Print(position p)
  85. {
  86. while (p != NULL)
  87. {
  88. printf("%d\t",p->num);
  89. p=p->next;
  90. }
  91. }
  92. //void Cross(position prs, position p, position q)
  93. //{
  94. // position tmp, z;
  95. // while( p!= NULL)
  96. // {
  97. // tmp = q;
  98. // while( tmp != NULL)
  99. // {
  100. // if (tmp->broj == p->broj)
  101. // {
  102. // z = (position)malloc(sizeof(_NODE));
  103. // if (z == NULL)
  104. // printf("Greska u alokaciji memorije.");
  105. //
  106. // z->broj = p->broj;
  107. //
  108. // z->next = prs->next;
  109. // prs->next = z;
  110. //
  111. // p = p->next;
  112. // tmp = tmp->next;
  113. // }
  114. // else
  115. // tmp=tmp->next;
  116. // }
  117. // p = p->next;
  118. // }
  119. //}
  120. //void Unija(position un, position p, position q)
  121. //{
  122. // position z, tmp;
  123. //
  124. // while( p != NULL && q != NULL)
  125. // {
  126. // z = (position)malloc(sizeof(_NODE));
  127. // if(z==NULL)
  128. // {
  129. // printf("Greska u alokaciji memorije.");
  130. // break;
  131. // }
  132. // else
  133. // {
  134. // if(p->broj > q->broj)
  135. // {
  136. // z->broj=p->broj;
  137. // p=p->next;
  138. // }
  139. // else if(p->broj < q->broj)
  140. // {
  141. // z->broj = q->broj;
  142. // q=q->next;
  143. // }
  144. // else
  145. // {
  146. // z->broj = p->broj;
  147. // p=p->next;
  148. // q=q->next;
  149. // }
  150. // if( un->broj == z->broj)
  151. // {
  152. // free(z);
  153. // }
  154. // else
  155. // {
  156. // z->next=un->next;
  157. // un->next=z;
  158. // un=z;
  159. // }
  160. // }
  161. // }
  162. // if( p == NULL)
  163. // {
  164. // tmp = p;
  165. // }
  166. // else
  167. // {
  168. // tmp = q;
  169. // }
  170. // while( tmp != NULL)
  171. // {
  172. // z=(position)malloc(sizeof(_NODE));
  173. //
  174. // if(un->broj == z->broj)
  175. // free(z);
  176. // else
  177. // {
  178. // z->next = un->next;
  179. // un->next = z;
  180. // un=z;
  181. // }
  182. //
  183. // tmp=tmp->next;
  184. // }
  185. //}
  186. void Union(position un, position p, position q)
  187. {
  188. position z,tmp;
  189.  
  190. while (p != NULL && q != NULL)
  191. {
  192.  
  193. z = (position)malloc(sizeof(_NODE));
  194. if (z == NULL)
  195. {
  196. printf("Memory allocation error!");
  197. break;
  198. }
  199. else
  200. {
  201. if (p->num < q->num)
  202. {
  203. z->num = p->num;
  204. z->next = un->next;
  205. un->next = z;
  206.  
  207. p = p->next;
  208. }
  209. else if (p->num > q->num)
  210. {
  211. z->num = q->num;
  212. z->next = un->next;
  213. un->next = z;
  214.  
  215. q = q->next;
  216. }
  217. else
  218. {
  219. z->num = p->num;
  220.  
  221. z->next = un->next;
  222. un->next = z;
  223.  
  224. p = p->next;
  225. q = q->next;
  226. }
  227. if (z->num == un->num)
  228. {
  229. un->next = z->next;
  230. free(z);
  231. }
  232. }
  233. }
  234. if (p == NULL)
  235. {
  236. tmp = q;
  237. while (tmp != NULL)
  238. {
  239. z = (position)malloc(sizeof(_NODE));
  240. if (z == NULL)
  241. {
  242. printf("Memory allocation error!.");
  243. break;
  244. }
  245. else
  246. {
  247. z->num = tmp->num;
  248. z->next = un->next;
  249. un->next = z;
  250. tmp = tmp->next;
  251. }
  252. }
  253. }
  254. if (q == NULL)
  255. {
  256. tmp = p;
  257. while (tmp != NULL)
  258. {
  259. z = (position)malloc(sizeof(_NODE));
  260. if (z == NULL)
  261. {
  262. printf("Memory allocation error!");
  263. break;
  264. }
  265. else
  266. {
  267. z->num = tmp->num;
  268. z->next = un->next;
  269. un->next = z;
  270. }
  271. tmp = tmp->next;
  272. }
  273. }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement