File: Stare vježbe/vjezbe12/72__studenti_u_listi.c

  1. /*
  2.   72__studenti_u_listi.c
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <malloc.h>
  8. #include <string.h>
  9.  
  10. typedef struct __student {
  11. char *ime, *prezime;
  12. int broj_indexa;
  13. float prosjek;
  14. struct __student *next;
  15. } student;
  16.  
  17. void ispis (student *);
  18. float prosjek (student *);
  19. student *sort (student *);
  20. student *unos (student *);
  21. void pocisti (student *);
  22. student *brisi (student *, int);
  23.  
  24. int main ( void ) {
  25. int opcija=0, br;
  26. student *pocetak=NULL;
  27.  
  28. while (opcija!=6) {
  29. printf ("\n\n-------------------------\n");
  30. printf ("1 - Unos novog studenta\n");
  31. printf ("2 - Ispis studenata\n");
  32. printf ("3 - Sortiranje liste\n");
  33. printf ("4 - Ispis prosjeka\n");
  34. printf ("5 - Brisanje studenta\n");
  35. printf ("6 - Kraj\n");
  36. printf ("-------------------------\n");
  37. scanf ("%d", &opcija);
  38.  
  39. switch (opcija) {
  40. case 1: pocetak=unos(pocetak); break;
  41. case 2: ispis(pocetak); break;
  42. case 3: pocetak=sort(pocetak); break;
  43. case 4: printf("Prosjek=%3.2f\n", prosjek(pocetak)); break;
  44. case 5: printf("Unesite broj indexa:");
  45. scanf("%d", &br);
  46. pocetak=brisi (pocetak, br);
  47. break;
  48. case 6: pocisti(pocetak);
  49. default: break;
  50. }
  51. }
  52.  
  53. return 0;
  54. }
  55.  
  56. void ispis (student *pocetak) {
  57. /* ispis (eventualno nesortirane) liste studenata */
  58. printf ("%15s %15s %6s %7s\n", "IME", "PREZIME", "BRIND", "PROSJEK");
  59.  
  60. while (pocetak!=NULL) {
  61. printf("%15s %15s %6d %7.2f\n",
  62. pocetak->ime, pocetak->prezime,
  63. pocetak->broj_indexa, pocetak->prosjek);
  64.  
  65. pocetak = pocetak->next;
  66. }
  67. }
  68.  
  69. float prosjek (student *pocetak) {
  70. /* racuna prosjek ocjena svih studenata u listi */
  71. int n=0;
  72. float f=0.0f;
  73.  
  74. while (pocetak!=NULL) {
  75. f += pocetak->prosjek;
  76. pocetak = pocetak->next;
  77. n++;
  78. }
  79.  
  80. if (n==0) return 0.0f;
  81. else return f/n;
  82. }
  83.  
  84. student *sort (student *prvi) {
  85. /* sortira listu studenata po prezimenima (bubble sort) */
  86. int nered=1; /* nered==1 <-> lista nije sortirana */
  87. student *trenutni, *prije, *iza;
  88.  
  89. /* ako je lista prazna ili ima samo 1 clan, nemamo sto sortirati */
  90. if (prvi==NULL || prvi->next==NULL) return prvi;
  91.  
  92. while (nered) {
  93. nered=0;
  94. trenutni=prvi;
  95. iza=prvi->next;
  96.  
  97. if (strcmp(trenutni->prezime, iza->prezime) > 0) {
  98. /* ako su prva elementa u krivom poredku, zamijeni ih */
  99. trenutni->next = iza->next;
  100. iza->next = trenutni;
  101. prvi=iza;
  102. nered = 1;
  103. }
  104.  
  105. prije = prvi;
  106. trenutni=prvi->next;
  107. iza = trenutni->next;
  108.  
  109. while (iza!=NULL) {
  110. if (strcmp(trenutni->prezime, iza->prezime) > 0) {
  111. /* ako su neka 2 elementa u krivom poredku, zamijeni ih */
  112. prije->next = iza;
  113. trenutni->next = iza->next;
  114. iza->next = trenutni;
  115. nered = 1;
  116. }
  117. prije = prije->next;
  118. trenutni = prije->next;
  119. iza = trenutni->next;
  120. }
  121. }
  122. return prvi;
  123. }
  124.  
  125. student *unos (student *pocetak)
  126. {
  127. /* ucitava podatke o novom studentu i ubacuje ga na pocetak liste;
  128.   vraca novi pocetak liste */
  129. student *novi;
  130. char temp[100];
  131.  
  132. /* alociraj prostor za novu zapis u listi */
  133. novi = (student *) malloc(sizeof(student));
  134.  
  135. printf ("Ime: ");
  136. scanf ("%s", temp);
  137. novi->ime = (char *) malloc((strlen(temp)+1) * sizeof(char));
  138. strcpy (novi->ime, temp);
  139.  
  140. printf ("Prezime: ");
  141. scanf ("%s", temp);
  142. novi->prezime = (char *) malloc((strlen(temp)+1) * sizeof(char));
  143. strcpy (novi->prezime, temp);
  144.  
  145. printf ("Broj indexa: ");
  146. scanf ("%d", &novi->broj_indexa);
  147.  
  148. printf ("Prosjek: ");
  149. scanf ("%f", &novi->prosjek);
  150.  
  151. /* ubacujem na pocetak liste */
  152. if (pocetak==NULL)
  153. novi->next = NULL;
  154. else novi->next = pocetak;
  155.  
  156. return novi;
  157. }
  158.  
  159. void pocisti (student *st)
  160. {
  161. /* oslobadja memoriju koju zauzimaju podaci u listi */
  162. student *temp;
  163.  
  164. while (st!=NULL){
  165. temp=st->next;
  166. free(st);
  167. st=temp;
  168. }
  169. }
  170.  
  171. student *brisi (student *pocetak, int br)
  172. {
  173. /* iz liste se uklanja student sa danim brojem index-a */
  174. /* vraca se pokazivac na pocetak liste */
  175. student *temp, *prethodni, *trenutni;
  176.  
  177. /* ako je lista prazna, nemamo sto brisati */
  178. if (pocetak==NULL) return pocetak;
  179.  
  180. if (br==pocetak->broj_indexa)
  181. {
  182. /* ako treba izbaciti studenta sa pocetka liste... */
  183. temp = pocetak->next;
  184. free (pocetak->ime);
  185. free (pocetak->prezime);
  186. free (pocetak);
  187. pocetak=temp;
  188.  
  189. return pocetak;
  190. }
  191. else
  192. {
  193. prethodni = pocetak;
  194. trenutni = pocetak->next;
  195. while(trenutni!=NULL)
  196. {
  197. if (br==trenutni->broj_indexa)
  198. {
  199. /* podaci studenta kojeg treba izbaciti su u cvoru trenutni */
  200. prethodni->next=trenutni->next;
  201. free (trenutni->ime);
  202. free (trenutni->prezime);
  203. free (trenutni);
  204. return pocetak;
  205. }
  206.  
  207. prethodni = trenutni;
  208. trenutni = trenutni->next;
  209. }
  210.  
  211. return pocetak;
  212. }
  213. }
  214.