Advertisement
mdalic

aisp labovi za 2

Feb 13th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.93 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #include <stdio.h>
  5.  
  6. #define MIN 1
  7. #define MAX 15
  8. #define CHARLEN 20
  9.  
  10. struct lista
  11. {
  12.   int brojIndexa;
  13.   char *ime;
  14.   char *prezime;
  15.   int orderNum;
  16.   struct lista *next;
  17. };
  18.  
  19. struct lista *CreateNode (int brojIndexa, char *ime, char *prezime,int orderNum);
  20. int InsertAfter (struct lista *head, int brojIndexa, char *ime, char *prezime,int orderNum);
  21. int IspisListe (struct lista *head);
  22. struct lista ReadFromFile ();
  23. int OrderNumGenerator ();
  24. int InsertLast(struct lista *head, int brojIndexa, char *ime, char *prezime,int orderNum);
  25. int SortedInput(struct lista *head, int brojIndexa, char *ime, char *prezime,int orderNum);
  26. int FillSortedInput(struct lista *nonSortedList,struct lista *sortedList);
  27. int FreeAllocatedData(struct lista *head);
  28. int DeleteNode(struct lista *head);
  29.  
  30.  
  31. int main ()
  32. {
  33.     struct lista head;
  34.     struct lista test;
  35.     test.next = NULL;
  36.     head.next = NULL;
  37.     head = ReadFromFile();
  38.     FillSortedInput(&head,&test);
  39.     IspisListe(&test);
  40.     FreeAllocatedData(&test);
  41.     FreeAllocatedData(&head);
  42.  
  43.   return 0;
  44. }
  45.  
  46.  
  47. struct lista * CreateNode (int brojIndexa, char *ime, char *prezime, int orderNum)
  48. {
  49.   struct lista *returnList;
  50.  
  51.   returnList = (struct lista *) malloc (sizeof (struct lista));
  52.  
  53.   if (returnList != NULL)
  54.     {
  55.       returnList->brojIndexa = brojIndexa;
  56.       returnList->ime = ime;
  57.       returnList->prezime = prezime;
  58.       returnList->orderNum = orderNum;
  59.       returnList->next = NULL;
  60.     }
  61.  
  62.   return returnList;
  63.  
  64. }
  65.  
  66.  
  67. int InsertAfter (struct lista *head, int brojIndexa, char *ime, char *prezime,
  68.          int orderNum)
  69. {
  70.   struct lista *temp;
  71.  
  72.   if (head == NULL)
  73.     return -1;
  74.  
  75.   temp = CreateNode (brojIndexa, ime, prezime, orderNum);
  76.   temp->next = head->next;
  77.   head->next = temp;
  78.  
  79.   return 0;
  80. }
  81.  
  82. int IspisListe (struct lista *head)
  83. {
  84.   if (head == NULL)
  85.     return -1;
  86.  
  87.   if (head->next == NULL)
  88.     return 1;
  89.  
  90.   head = head->next;
  91.  
  92.   while (head->next != NULL)
  93.     {
  94.       printf ("%d %s %s %d\n", head->brojIndexa, head->ime, head->prezime,
  95.           head->orderNum);
  96.       head = head->next;
  97.     }
  98.   printf ("%d %s %s %d\n", head->brojIndexa, head->ime, head->prezime,
  99.       head->orderNum);
  100.   return 0;
  101. }
  102.  
  103. int OrderNumGenerator ()
  104. {
  105.   int x = 0;
  106.   x = rand () % (MAX + 1 - MIN) + MIN;
  107.   return x;
  108. }
  109.  
  110. struct lista ReadFromFile ()
  111. {
  112.   char *ime;
  113.   char *prezime;
  114.   int brojIndexa;
  115.   int orderNum;
  116.   FILE *fp;
  117.   struct lista head;
  118.  
  119.   head.next = NULL;
  120.   ime = (char *) malloc (sizeof (char) * CHARLEN);
  121.   prezime = (char *) malloc (sizeof (char) * CHARLEN);
  122.  
  123.  
  124.   fp = fopen ("F:\\projects\\kolok\\asdf.txt", "r");
  125.  
  126.  
  127.     while(!feof(fp))
  128.     {
  129.         fscanf(fp,"%d %s %s",&brojIndexa,ime,prezime);
  130.         orderNum = OrderNumGenerator();
  131.         InsertLast(&head,brojIndexa,ime,prezime,orderNum);
  132.  
  133.         ime = (char *) malloc (sizeof (char) * CHARLEN);
  134.         prezime = (char *) malloc (sizeof (char) * CHARLEN);
  135.     }
  136.  
  137.  
  138.     fclose(fp);
  139.     return head;
  140.  
  141. }
  142.  
  143.  
  144.  
  145. int InsertLast(struct lista *head, int brojIndexa, char *ime, char *prezime,int orderNum)
  146. {
  147.     if(head == NULL)
  148.         return -1;
  149.  
  150.     while(head->next != NULL)
  151.         head = head->next;
  152.  
  153.     InsertAfter(head,brojIndexa,ime,prezime,orderNum);
  154.  
  155.     return 0;
  156. }
  157.  
  158. int SortedInput(struct lista *head, int brojIndexa, char *ime, char *prezime,int orderNum)
  159. {
  160.     struct lista *temp;
  161.     if(head == NULL)
  162.         return -1;
  163.  
  164.     temp = head->next;
  165.     while(head->next != NULL && temp != NULL && strcmp(temp->prezime,prezime)<0)
  166.     {
  167.         head = head->next;
  168.         temp = temp->next;
  169.     }
  170.     InsertAfter(head,brojIndexa,ime,prezime,orderNum);
  171.     return 0;
  172. }
  173.  
  174. int FillSortedInput(struct lista *nonSortedList,struct lista *sortedList)
  175. {
  176.     if(nonSortedList == NULL || sortedList == NULL)
  177.         return -1;
  178.     if(nonSortedList->next == NULL)
  179.         return 1;
  180.  
  181.     nonSortedList = nonSortedList->next;
  182.     while(nonSortedList->next != NULL)
  183.     {
  184.         SortedInput(sortedList,nonSortedList->brojIndexa,nonSortedList->ime,nonSortedList->prezime,nonSortedList->orderNum);
  185.         nonSortedList = nonSortedList->next;
  186.  
  187.     }
  188.     SortedInput(sortedList,nonSortedList->brojIndexa,nonSortedList->ime,nonSortedList->prezime,nonSortedList->orderNum);
  189.  
  190.     return 0;
  191. }
  192.  
  193. int FreeAllocatedData(struct lista *head)
  194. {
  195.     struct lista *start;
  196.     struct lista *temp;
  197.     if(head == NULL)
  198.         return 0;
  199.  
  200.     start = head;
  201.  
  202.     while(start->next != NULL)
  203.     {
  204.         temp = head;
  205.         while (temp->next != NULL)
  206.         {
  207.             head = temp;
  208.             temp = temp->next;
  209.  
  210.         }
  211.         DeleteNode(head);
  212.         head = start;
  213.     }
  214.     return 0;
  215. }
  216.  
  217.  
  218. int DeleteNode(struct lista *head)
  219. {
  220.     struct lista *temp;
  221.     if(head == NULL)
  222.         return 0;
  223.  
  224.  
  225.     temp = head->next;
  226.     head->next = temp->next;
  227.  
  228.     free(temp);
  229.     return 0;
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement