Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define STRING_MAX_LENGTH (50)
  5.  
  6. typedef struct _osoba* Position;
  7.  
  8. typedef struct _osoba
  9. {
  10. char ime[STRING_MAX_LENGTH];
  11. char prezime[STRING_MAX_LENGTH];
  12. int godinaRodenja;
  13. Position next;
  14. }Student;
  15.  
  16. typedef struct _studentPodaci
  17. {
  18. char ime[STRING_MAX_LENGTH];
  19. char prezime[STRING_MAX_LENGTH];
  20. int godinaRodenja;
  21. }StudentPodaci;
  22.  
  23. int InsertAfter(Position head, StudentPodaci studentPodaci);
  24. int InsertEnd(Position position, StudentPodaci studentPodaci);
  25. void IspisListe(Position head);
  26. int DeleteElement(Position position);
  27. Position FindPreviousElement(Position head, char *prezime);
  28.  
  29. int IspisIzbornika();
  30. void PrintElement(Position position);
  31. StudentPodaci GetStudentDataFromConsole();
  32. void FreeAlocatedData(Position head);
  33.  
  34. int main(int agrc, char *argv[])
  35. {
  36. char izbor = 0;
  37. char prezime[STRING_MAX_LENGTH];
  38. Student head;
  39. StudentPodaci bufferStudent;
  40. Position bufferPosition;
  41.  
  42. head.next = NULL;
  43.  
  44. while (izbor != 'K' && izbor != 'k')
  45. {
  46. IspisIzbornika();
  47. scanf(" %c", &izbor);
  48. switch (izbor)
  49. {
  50. case '1':
  51. bufferStudent = GetStudentDataFromConsole();
  52. InsertAfter(&head, bufferStudent);
  53. break;
  54. case '2':
  55. IspisListe(&head);
  56. break;
  57. case '3':
  58. bufferStudent = GetStudentDataFromConsole();
  59. InsertEnd(&head, bufferStudent);
  60. break;
  61. case '4':
  62. printf("\nUnesi prezime studenta cije podatke ispisujemo: ");
  63. scanf("%s", prezime);
  64. bufferPosition = FindPreviousElement(&head, prezime);
  65. if (bufferPosition != NULL)
  66. PrintElement(bufferPosition->next);
  67. else
  68. printf("\nElement ne postoji u listi.");
  69. break;
  70. case '5':
  71. break;
  72. case 'k':
  73. case 'K':
  74. break;
  75. default:
  76. printf("\r\nPogresan izbor <%c>.\r\nPokusajte ponovno.\r\n", izbor);
  77. break;
  78. }
  79. }
  80.  
  81. //FreeAlocatedData(&head);
  82. IspisListe(&head);
  83. }
  84.  
  85. int IspisIzbornika()
  86. {
  87. printf("\r\n\r\n");
  88. printf("\t1. Unos elementa u listu\r\n");
  89. printf("\t2. Ispis liste\r\n");
  90. printf("\t3. Unos na kraj\r\n");
  91. printf("\t4. Pronadji po prezimenu\r\n");
  92. printf("\t5. Izbrisi po prezimenu\r\n");
  93. printf("\tK. Izlaz iz programa\r\n");
  94. printf("\r\n\tIzbor : ");
  95.  
  96. return 0;
  97. }
  98.  
  99. int InsertAfter(Position position, StudentPodaci studentPodaci)
  100. {
  101. Position newItem = NULL;
  102. if (position == NULL)
  103. return -1;
  104.  
  105. newItem = (Position)malloc(sizeof(Student));
  106.  
  107. if (newItem == NULL)
  108. return -2;
  109.  
  110. //inicijalizacija alocirane strukture
  111. newItem->next = NULL;
  112. newItem->godinaRodenja = studentPodaci.godinaRodenja;
  113. strcpy(newItem->ime, studentPodaci.ime);
  114. strcpy(newItem->prezime, studentPodaci.prezime);
  115. //unos u listu
  116. newItem->next = position->next;
  117. position->next = newItem;
  118.  
  119. return 0;
  120. }
  121.  
  122. int InsertEnd(Position position, StudentPodaci studentPodaci)
  123. {
  124. while(position->next != NULL)
  125. position = position->next;
  126.  
  127. InsertAfter(position, studentPodaci);
  128.  
  129. return 0;
  130. }
  131.  
  132. void IspisListe(Position head)
  133. {
  134. while(head->next != NULL)
  135. {
  136. printf("%s %s %d", head->next->ime, head->next->prezime, head->next->godinaRodenja);
  137. printf("\n");
  138. head = head->next;
  139. }
  140. }
  141.  
  142. int DeleteElement(Position position)
  143. {
  144. Position temp = NULL;
  145. if((position->next != NULL || position->next->next != NULL) && position->next->next->ime != NULL)
  146. {
  147. temp = position->next;
  148. free(position->next);
  149. position = temp;
  150. return 1;
  151. }
  152. else
  153. return -1;
  154. }
  155.  
  156. Position FindPreviousElement(Position head, char *prezime)
  157. {
  158. Position temp = NULL;
  159. if ((head->next != NULL || head->next->next != NULL) && head->next->next->prezime != NULL);
  160. temp = head->next;
  161.  
  162. return temp;
  163. }
  164.  
  165. void PrintElement(Position position)
  166. {
  167. if(position->next != NULL)
  168. printf("%s %s %d /n", position->ime, position->prezime, position->godinaRodenja);
  169. }
  170.  
  171. StudentPodaci GetStudentDataFromConsole()
  172. {
  173. StudentPodaci tempStudent;
  174.  
  175. printf("Unesi ime: ");
  176. scanf(" %s", tempStudent.ime);
  177. printf("Unesi prezime: ");
  178. scanf(" %s", tempStudent.prezime);
  179. printf("Unesi godinu rodenja: ");
  180. scanf(" %d", &tempStudent.godinaRodenja);
  181.  
  182. return tempStudent;
  183. }
  184.  
  185. void FreeAlocatedData(Position head)
  186. {
  187. free(head);
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement