Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<time.h>
  6.  
  7. typedef struct Student ST;
  8.  
  9. struct Student {
  10.     char ime[15];
  11.     char prezime[15];
  12.     int indeks;
  13.     int orderNum;
  14.     struct Student *next;
  15. };
  16.  
  17. ST *Alloc(ST *newitem);
  18. int Insert(ST *head, ST *newitem);
  19. int ReadFIle(ST *head);
  20. int Print(ST *head);
  21. int Kopiraj(ST *head, ST* sort);
  22. int Copy(ST *sort, ST* head);
  23. int SortInput(ST *sort, ST *newitem);
  24. int Brisi_iste(ST *sort);
  25. int Delete(ST *sort, ST *head);
  26. int DeleteList(ST *sort);
  27.  
  28. int main(void)
  29. {
  30.     ST head = { 0 };
  31.     ST sort_head = { 0 };
  32.  
  33.     srand((unsigned)time);
  34.  
  35.     head.next = NULL;
  36.     sort_head.next = NULL;
  37.  
  38.     //procitaj indeks,ime i prezime iz dtk
  39.     ReadFIle(&head);
  40.  
  41.     // a. napravi novu listu i spremi studente + orderNum
  42.     Kopiraj(&head, &sort_head);
  43.     Print(&sort_head);
  44.  
  45.     // b. izbrisi studente s istim orderNum
  46.     Brisi_iste(&sort_head);
  47.     Print(&sort_head);
  48.  
  49.     //Izbrisi liste ( oslobodi mem )
  50.     DeleteList(&head);
  51.     DeleteList(&sort_head);
  52.     Print(&sort_head);
  53.  
  54.  
  55.     return 0;
  56. }
  57.  
  58. ST *Alloc(ST *newitem)
  59. {
  60.     newitem = (ST*)malloc(sizeof(ST));
  61.  
  62.     if (newitem == NULL)
  63.     {
  64.         printf("GRESKA1 !");
  65.         return NULL;
  66.     }
  67.  
  68.     return newitem;
  69. }
  70. int Insert(ST *head, ST *newitem)
  71. {
  72.     newitem->next = head->next;
  73.     head->next = newitem;
  74.  
  75.     return 0;
  76. }
  77. int Print(ST *head)
  78. {
  79.     head = head->next;
  80.  
  81.     if (head == NULL)
  82.     {
  83.         printf("Empty list\n");
  84.         return 0;
  85.     }
  86.  
  87.     printf("Ispis liste:\n");
  88.     while (head != NULL)
  89.     {
  90.         printf("%d %s %s %d\n", head->indeks, head->ime, head->prezime, head->orderNum);
  91.         head = head->next;
  92.     }
  93.     return 0;
  94. }
  95. int ReadFIle(ST *head)
  96. {
  97.     FILE *fp = NULL;
  98.     ST *newitem = NULL;
  99.  
  100.     fp = fopen("Studenti2.txt", "r");
  101.     if (fp == NULL)
  102.     {
  103.         printf("GRESKA2!");
  104.         return 0;
  105.     }
  106.  
  107.     while (!(feof(fp)))
  108.     {
  109.         newitem = Alloc(newitem);
  110.         fscanf(fp, "%d %s %s", &newitem->indeks, newitem->ime, newitem->prezime);
  111.         newitem->orderNum = rand() % 5 + 1;
  112.         Insert(head, newitem);
  113.     }
  114.  
  115.     fclose(fp);
  116.  
  117.     return 0;
  118. }
  119. int Kopiraj(ST *head, ST* sort)
  120. {
  121.     head = head->next;
  122.  
  123.     while (head != NULL)
  124.     {
  125.         Copy(sort, head);
  126.         head = head->next;
  127.     }
  128.  
  129.     return 0;
  130. }
  131. int Copy(ST *sort, ST* head)
  132. {
  133.     ST *newitem = NULL;
  134.  
  135.     newitem = Alloc(newitem);
  136.  
  137.     newitem->indeks = head->indeks;
  138.     newitem->orderNum = head->orderNum;
  139.     strcpy(newitem->ime, head->ime);
  140.     strcpy(newitem->prezime, head->prezime);
  141.  
  142.     SortInput(sort, newitem);
  143.  
  144.     return 0;
  145. }
  146. int SortInput(ST *sort, ST *newitem)
  147. {
  148.     ST *tmp = NULL;
  149.  
  150.     tmp = sort;
  151.     sort = sort->next;
  152.  
  153.     while (sort != NULL && strcmp(sort->prezime, newitem->prezime) < 0)
  154.     {
  155.         tmp = sort;
  156.         sort = sort->next;
  157.     }
  158.  
  159.     Insert(tmp, newitem);
  160.  
  161.     return 0;
  162. }
  163. int Brisi_iste(ST *sort)
  164. {
  165.     ST *prev_sort = NULL;
  166.     int i = 0;
  167.  
  168.  
  169.     prev_sort = sort;
  170.     sort = sort->next;
  171.  
  172.     while (sort != NULL)
  173.     {
  174.         i = Delete(sort, sort);
  175.  
  176.         if (i != 0)
  177.         {
  178.             prev_sort->next = sort->next;
  179.             free(sort);
  180.             sort = prev_sort->next;
  181.             continue;
  182.  
  183.         }
  184.         else
  185.         {
  186.             prev_sort = sort;
  187.             sort = sort->next;
  188.         }
  189.     }
  190.  
  191.  
  192.     return 0;
  193. }
  194. int Delete(ST *sort, ST *head)
  195. {
  196.     ST *prev_head = NULL;
  197.     int i = 0;
  198.     ST *tmp = NULL;
  199.  
  200.     tmp = head;
  201.     prev_head = head;
  202.     head = head->next;
  203.  
  204.     while (head != NULL)
  205.     {
  206.         if (sort->orderNum == head->orderNum)
  207.         {
  208.             prev_head->next = head->next;
  209.             free(head);
  210.             head = prev_head->next;
  211.             i = 1;
  212.         }
  213.         else
  214.         {
  215.             prev_head = head;
  216.             head = head->next;
  217.         }
  218.  
  219.     }
  220.  
  221.     //Print(tmp);
  222.  
  223.     return i;
  224. }
  225. int DeleteList(ST *sort)
  226. {
  227.     ST *head = NULL;
  228.  
  229.     head = sort;
  230.     sort = sort->next;
  231.  
  232.     while (head->next != NULL)
  233.     {
  234.         head->next = sort->next;
  235.         free(sort);
  236.         sort = head->next;
  237.     }
  238.  
  239.     return 0;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement