Advertisement
100rads

vezaneListe mk2

Oct 23rd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.08 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #define STRING_MAX_LENGTH (50)
  6.  
  7. typedef struct _osoba* Position;
  8.  
  9. typedef struct _osoba
  10. {
  11.     char ime[STRING_MAX_LENGTH];
  12.     char prezime[STRING_MAX_LENGTH];
  13.     int godinaRodenja;
  14.     Position next;
  15. }Student;
  16.  
  17. typedef struct _studentPodaci
  18. {
  19.     char ime[STRING_MAX_LENGTH];
  20.     char prezime[STRING_MAX_LENGTH];
  21.     int godinaRodenja;
  22. }StudentPodaci;
  23.  
  24. int InsertAfter(Position head, StudentPodaci studentPodaci);
  25. int InsertEnd(Position position, StudentPodaci studentPodaci);
  26. void IspisListe(Position head);
  27. int DeleteElement(Position position);
  28. Position FindPreviousElement(Position head, char *prezime);
  29.  
  30. int IspisIzbornika();
  31. void PrintElement(Position position);
  32. StudentPodaci GetStudentDataFromConsole();
  33. void FreeAlocatedData(Position head);
  34.  
  35. int InsertBefore(Position position, StudentPodaci studentPodaci);
  36. int SwapElements(Position prev, Position first);
  37. int WriteIntoFile(Position head, char name);
  38. int SortList(Position head);
  39. int ElementCounter(Position head);
  40.  
  41. int main(int agrc, char *argv[])
  42. {
  43.     char izbor = 0;
  44.     char prezime[STRING_MAX_LENGTH];
  45.     char fileName[STRING_MAX_LENGTH] = "lista.txt";
  46.     Student head;
  47.     StudentPodaci bufferStudent;
  48.     Position bufferPosition;
  49.  
  50.     head.next = NULL;
  51.  
  52.     while (izbor != 'K' && izbor != 'k')
  53.     {
  54.         IspisIzbornika();
  55.         scanf(" %c", &izbor);
  56.         switch (izbor)
  57.         {
  58.         case '1':
  59.             bufferStudent = GetStudentDataFromConsole();
  60.             InsertAfter(&head, bufferStudent);
  61.             break;
  62.         case '2':
  63.             IspisListe(&head);
  64.             break;
  65.         case '3':
  66.             bufferStudent = GetStudentDataFromConsole();
  67.             InsertEnd(&head, bufferStudent);
  68.             break;
  69.         case '4':
  70.             printf("\nUnesi prezime studenta cije podatke ispisujemo: ");
  71.             scanf("%s", prezime);
  72.             bufferPosition = FindPreviousElement(&head, prezime);
  73.             if (bufferPosition != NULL)
  74.                 PrintElement(bufferPosition->next);
  75.             else
  76.                 printf("\nElement ne postoji u listi.");
  77.             break;
  78.         case '5':
  79.             break;
  80.         case '6':
  81.             WriteIntoFile(&head, fileName);
  82.             break;
  83.         case 'k':
  84.         case 'K':
  85.             break;
  86.         default:
  87.             printf("\r\nPogresan izbor <%c>.\r\nPokusajte ponovno.\r\n", izbor);
  88.             break;
  89.         }
  90.     }
  91.  
  92.     FreeAlocatedData(&head);
  93.     IspisListe(&head);
  94. }
  95.  
  96. int IspisIzbornika()
  97. {
  98.     printf("\r\n\r\n");
  99.     printf("\t1. Unos elementa u listu\r\n");
  100.     printf("\t2. Ispis liste\r\n");
  101.     printf("\t3. Unos na kraj\r\n");
  102.     printf("\t4. Pronadji po prezimenu\r\n");
  103.     printf("\t5. Izbrisi po prezimenu\r\n");
  104.     printf("\t6. Upisi u datoteku\r\n");
  105.     printf("\tK. Izlaz iz programa\r\n");
  106.     printf("\r\n\tIzbor : ");
  107.  
  108.     return 0;
  109. }
  110.  
  111. int InsertAfter(Position position, StudentPodaci studentPodaci)
  112. {
  113.     Position newItem = NULL;
  114.     if (position == NULL)
  115.         return -1;
  116.  
  117.     newItem = (Position)malloc(sizeof(Student));
  118.  
  119.     if (newItem == NULL)
  120.         return -2;
  121.  
  122.     //inicijalizacija alocirane strukture
  123.     newItem->next = NULL;
  124.     newItem->godinaRodenja = studentPodaci.godinaRodenja;
  125.     strcpy(newItem->ime, studentPodaci.ime);
  126.     strcpy(newItem->prezime, studentPodaci.prezime);
  127.     //unos u listu
  128.     newItem->next = position->next;
  129.     position->next = newItem;
  130.  
  131.     return 0;
  132. }
  133.  
  134. int InsertEnd(Position position, StudentPodaci studentPodaci)
  135. {
  136.     while (position->next != NULL)
  137.         position = position->next;
  138.  
  139.     InsertAfter(position, studentPodaci);
  140.  
  141.     return 0;
  142. }
  143.  
  144. void IspisListe(Position head)
  145. {
  146.     while (head->next != NULL)
  147.     {
  148.         printf("%s %s %d", head->next->ime, head->next->prezime, head->next->godinaRodenja);
  149.         printf("\n");
  150.         head = head->next;
  151.     }
  152. }
  153.  
  154. int DeleteElement(Position position)
  155. {
  156.     Position temp = NULL;
  157.     if ((position->next != NULL || position->next->next != NULL) && position->next->next->ime != NULL)
  158.     {
  159.         temp = position->next;
  160.         free(position->next);
  161.         position = temp;
  162.         return 1;
  163.     }
  164.     else
  165.         return -1;
  166. }
  167.  
  168. Position FindPreviousElement(Position head, char *prezime)
  169. {
  170.     Position temp = NULL;
  171.     if ((head->next != NULL || head->next->next != NULL) && head->next->next->prezime != NULL)
  172.         temp = head->next;
  173.  
  174.     return temp;
  175. }
  176.  
  177. void PrintElement(Position position)
  178. {
  179.     if (position->next != NULL)
  180.         printf("%s %s %d /n", position->ime, position->prezime, position->godinaRodenja);
  181. }
  182.  
  183. StudentPodaci GetStudentDataFromConsole()
  184. {
  185.     StudentPodaci tempStudent;
  186.  
  187.     printf("Unesi ime, prezime i godinu rodenja: \n");
  188.     scanf(" %s %s %d", tempStudent.ime, tempStudent.prezime, &tempStudent.godinaRodenja);
  189.     printf("\n");
  190.  
  191.     return tempStudent;
  192. }
  193.  
  194. void FreeAlocatedData(Position head)
  195. {
  196.     Position temp = NULL;
  197.     temp = head;
  198.     while (temp->next != NULL)
  199.     {
  200.         free(temp->ime);
  201.         free(temp->prezime);
  202.         free(temp->godinaRodenja);
  203.     }
  204.     free(head);
  205. }
  206.  
  207. int SwapElements(Position prev, Position first)
  208. {
  209.     Position temp = first->next;
  210.  
  211.     prev->next = first->next;
  212.  
  213.     if (temp == NULL)
  214.     {
  215.         return -1;
  216.     }
  217.  
  218.     first->next = temp->next;
  219.     temp->next = first;
  220.     return 0;
  221. }
  222.  
  223. int WriteIntoFile(Position head, char name)
  224. {
  225.     FILE *f;
  226.     f = fopen(name, "w");
  227.  
  228.     if (f == NULL)
  229.     {
  230.         return -1;
  231.     }
  232.     else
  233.     {
  234.         head = head->next;
  235.         while (head != NULL)
  236.         {
  237.             fprintf(f, "%s %s %d \n", head->ime, head->prezime, head->godinaRodenja);
  238.             head = head->next;
  239.         }
  240.         fclose(f);
  241.         return 0;
  242.     }
  243. }
  244.  
  245. int ElementCounter(Position head)
  246. {
  247.     int counter = 0;
  248.     head->next;
  249.     while (head->next != NULL)
  250.     {
  251.         counter++;
  252.     }
  253.  
  254.     return counter;
  255. }
  256.  
  257. int InsertBefore(Position position, StudentPodaci studentPodaci)
  258. {
  259.     Position temp = NULL;
  260.     if ((position->next != NULL || position->next->next != NULL))
  261.         temp = position->next;
  262.  
  263.     InsertAfter(temp, studentPodaci);
  264.  
  265.     return 0;
  266. }
  267.  
  268. int SortList(Position head)
  269. {
  270.     int i = 0;
  271.     int j = 0;
  272.     int n = ElementCounter(head);
  273.  
  274.     for (i = 0; i < j - 1; i++)
  275.         for (j = 0; j < i - n - 1; j++)
  276.             SwapElements(head, head->next);
  277.  
  278.     return 0;
  279. }
  280.  
  281. int ReadFromFile(Position head, char fileName)
  282. {
  283.     FILE *f;
  284.     f = fopen(fileName, "r");
  285.     StudentPodaci temp;
  286.  
  287.     if (f == NULL)
  288.     {
  289.         return -1;
  290.     }
  291.     else
  292.     {
  293.         head->next;
  294.         while(!feof(f))
  295.         {
  296.             fscanf(f, "%s %s %d", temp.ime, temp.prezime, &temp.godinaRodenja);
  297.             InsertAfter(head, temp);
  298.         }
  299.         fclose(f);
  300.         return 0;
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement