Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #define MAX_NAZIV 30
  6. #define MAX 15
  7. #define MIN 1
  8.  
  9. typedef struct student*Position;
  10. typedef struct student
  11. {
  12. int brojIndexa;
  13. char *ime;
  14. char *prezime;
  15. int orderNum;
  16. Position next;
  17. }Student;
  18.  
  19. Position KreirajClan();
  20. int CitanjeIzFilea(Position);
  21. int RandomBroj();
  22. int FreeData(Position);
  23. int IspisListe(Position);
  24. int SortiranjeUnosa(Position, int, char*, char*, int);
  25. int BrisanjeIstih(Position);
  26. int IzbrisiClanove(Position , int );
  27. int SortirajClanove(Position);
  28.  
  29. int main()
  30. {
  31.  
  32. Student root;
  33. root.next = NULL;
  34. srand((unsigned)time(NULL));
  35. printf("\t------Lista studenata------\n\n");
  36. CitanjeIzFilea(&root);
  37. IspisListe(&root);
  38. printf("\n\n\t------Lista studenata s jedistvenim orderNumb------\n\n");
  39. BrisanjeIstih(&root);
  40. IspisListe(&root);
  41. printf("\n\n\t------Lista studenata s jedistvenim orderNumb (sortirana)------\n\n");
  42. SortirajClanove(&root);
  43. IspisListe(&root);
  44. printf("\n\n\t------Brisanje liste------\n\n");
  45. FreeData(&root);
  46. IspisListe(&root);
  47.  
  48. return 0;
  49. }
  50.  
  51. int CitanjeIzFilea(Position p)
  52. {
  53. FILE*fp;
  54. Position newItem;
  55. int broj=0;
  56. int rndBr = 0;
  57. char *ime=(char*)malloc(sizeof(char)*MAX_NAZIV);
  58. char *prezime = (char*)malloc(sizeof(char)*MAX_NAZIV);
  59.  
  60. fp = fopen("studenti.txt", "r");
  61. if (fp == NULL)
  62. {
  63. printf("Datoteka se nije uspjesno otvorila");
  64. return -1;
  65. }
  66. while (!feof(fp))
  67. {
  68. ime = (char*)malloc(MAX_NAZIV * sizeof(char));
  69. prezime = (char*)malloc(MAX_NAZIV * sizeof(char));
  70.  
  71.  
  72. fscanf(fp, "%d %s %s",&broj, ime,prezime);
  73. rndBr = RandomBroj();
  74. if (p->next == NULL)
  75. {
  76. Position newItem;
  77. newItem = KreirajClan();
  78. newItem->brojIndexa = broj;
  79. newItem->ime = ime;
  80. newItem->prezime = prezime;
  81. newItem->orderNum = rndBr;
  82. newItem->next = p->next;
  83. p->next = newItem;
  84. }
  85. else
  86. {
  87.  
  88. SortiranjeUnosa(p, broj, ime, prezime, rndBr);
  89. }
  90.  
  91. }
  92. fclose(fp);
  93. return 0;
  94. }
  95. int SortiranjeUnosa(Position p, int index, char*ime, char*prezime, int rnd)
  96. {
  97. p = p->next;
  98. Position newItem;
  99. newItem = KreirajClan();
  100. newItem->brojIndexa = index;
  101. newItem->ime = ime;
  102. newItem->prezime = prezime;
  103. newItem->orderNum = rnd;
  104. newItem->next = NULL;
  105.  
  106.  
  107. while (p->next!=NULL)
  108. {
  109. if (strcmp(newItem->prezime, p->next->prezime) < 0)
  110. {
  111. newItem->next = p->next;
  112. p->next = newItem;
  113. return 0;
  114. }
  115. else
  116. {
  117. p = p->next;
  118.  
  119. }
  120.  
  121. }
  122. if (newItem->next == NULL)
  123. {
  124. p->next = newItem->next;
  125. p->next = newItem;
  126. }
  127.  
  128. return 0;
  129. }
  130.  
  131. Position KreirajClan()
  132. {
  133. Position temp;
  134.  
  135. temp = (Position)malloc(sizeof(Student));
  136. temp->brojIndexa = 0;
  137. temp->orderNum = NULL;
  138.  
  139. return temp;
  140. }
  141.  
  142. int RandomBroj()
  143. {
  144. int broj;
  145.  
  146. broj = rand() % (MAX - MIN + 1) + MIN;
  147.  
  148. return broj;
  149. }
  150. int BrisanjeIstih(Position p)
  151. {
  152. Position i=NULL;
  153. int broj;
  154. int brojac;
  155.  
  156. for (broj=1; broj < 16; broj++)
  157. {
  158. i = p;
  159. brojac = 0;
  160. while (i!=NULL)
  161. {
  162. if (i->orderNum == broj)
  163. {
  164. brojac++;
  165. }
  166.  
  167. i= i->next;
  168. }
  169. if (brojac > 1)
  170. {
  171. IzbrisiClanove(p, broj);
  172. }
  173.  
  174. }
  175. return 0;
  176. }
  177. int IzbrisiClanove(Position p, int br)
  178. {
  179. Position previ = NULL;
  180. Position i = NULL;
  181. Position temp=NULL;
  182.  
  183.  
  184. while (p->next!=NULL)
  185. {
  186.  
  187. if (br == p->next->orderNum)
  188. {
  189. temp = p->next;
  190. p->next = temp->next;
  191. free(temp);
  192.  
  193. }
  194. else
  195. {
  196. p = p->next;
  197. }
  198. }
  199. return 0;
  200. }
  201. int SortirajClanove(Position p)
  202. {
  203. Position i = NULL;
  204. Position j = NULL;
  205. Position prevj = NULL;
  206. Position temp = NULL;
  207. Position end=NULL;
  208.  
  209. i = p;
  210.  
  211. while (i->next!=end)
  212. {
  213. prevj = i;
  214. j = i->next;
  215. while (j->next!=end)
  216. {
  217. if (j->orderNum > j->next->orderNum)
  218. {
  219. temp = j->next;
  220. prevj->next = temp;
  221. j->next = temp->next;
  222. temp->next = j;
  223. j = temp;
  224. }
  225. prevj = j;
  226. j=j->next;
  227. }
  228. end = j;
  229. }
  230. return 0;
  231. }
  232. int IspisListe(Position p)
  233. {
  234. if (p->next == NULL)
  235. {
  236. printf("\t Lista je prazna!!\n\n");
  237. return -1;
  238. }
  239. p = p->next;
  240. printf("\n INDEKS\tIME\t\tPREZIME\t\tORDERnumb\n\n\n");
  241. while (p != NULL)
  242. {
  243.  
  244. printf(" %d\t%s\t\t%s\t\t%d\n", p->brojIndexa, p->ime, p->prezime, p->orderNum);
  245. p = p->next;
  246. }
  247. return 0;
  248. }
  249. int FreeData(Position p)
  250. {
  251.  
  252. Position temp = NULL;
  253. while (p->next!=NULL)
  254. {
  255. temp = p->next;
  256. p->next = temp->next;
  257. free(temp);
  258. }
  259. return 0;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement