Advertisement
mdalic

aisp 1.kolok bez stog/red/polinomi

Nov 21st, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define MIN_RAND 0
  7. #define MAX_RAND 30
  8. #define BROJ_RANNDOM_BROJEVA 10
  9. struct list{
  10.     int broj;
  11.     struct list* next;
  12. };
  13. struct list* CreateNode(int broj);
  14. int InsertAfter(struct list* head,int nodeInfo);
  15. int InsertLast(struct list* head,int nodeInfo);
  16. int IspisListe(struct list* head);
  17. struct list* FindNode(struct list* head, int broj);
  18. int DeleteNode(struct list* head,int broj);
  19. struct list* FindPrevNode(struct list* head, int broj);
  20. int InsertBefore(struct list* head,int nodeBroj,int newBroj);
  21. int FreeAlloData(struct list* head);
  22. int SortedInput(struct list* head,int node);
  23. int RandomFillList(struct list* head);
  24. int Unija(struct list* list1,struct list* list2, struct list* unija);
  25. int DoesNodeExist(struct list* head,int node);
  26. int Presjek(struct list* list1,struct list* list2, struct list* presjek);
  27.  
  28. int main() {
  29.     struct list list1;
  30.     struct list list2;
  31.     struct list unija;
  32.     struct list presjek;
  33.  
  34.  
  35.  
  36.     srand((unsigned) time(NULL));
  37.  
  38.     RandomFillList(&list1);
  39.     RandomFillList(&list2);
  40.  
  41.     Unija(&list1,&list2,&unija);
  42.     Presjek(&list1,&list2,&presjek);
  43.  
  44.     IspisListe(&list1);
  45.     IspisListe(&list2);
  46.     IspisListe(&unija);
  47.     IspisListe(&presjek);
  48. /*
  49.     FreeAlloData(&list1);
  50.     FreeAlloData(&list2);
  51.     FreeAlloData(&unija);
  52.     FreeAlloData(&presjek);
  53. */
  54.  
  55.     return 0;
  56. }
  57.  
  58. struct list* CreateNode(int broj){
  59.     struct list* returnList;
  60.     returnList =(struct list*) malloc(sizeof(struct list));
  61.     if(returnList != NULL) {
  62.         returnList->broj = broj;
  63.         returnList->next = NULL;
  64.     }
  65.  
  66.     return returnList;
  67.  
  68. }
  69. int InsertAfter(struct list* head,int nodeInfo){
  70.     struct list* temp;
  71.  
  72.     if(head == NULL)
  73.         return -1;
  74.  
  75.     temp = CreateNode(nodeInfo);
  76.     temp->next = head->next;
  77.     head->next = temp;
  78.  
  79.     return 0;
  80.     }
  81.  
  82. int InsertLast(struct list* head,int nodeInfo){
  83.     if(head == NULL)
  84.         return -1;
  85.  
  86.     while(head->next != NULL)
  87.         head = head->next;
  88.  
  89.     InsertAfter(head,nodeInfo);
  90.     return 0;
  91. }
  92.  
  93. int IspisListe(struct list* head){
  94.     if(head == NULL)
  95.         return -1;
  96.  
  97.     if(head->next == NULL)
  98.         return 0;
  99.  
  100.     head = head->next;
  101.  
  102.     while(head != NULL){
  103.  
  104.  
  105.         if(head->next == NULL)
  106.             break;
  107.         printf("%d\t",head->broj);
  108.         head = head->next;
  109.  
  110.  
  111.     }
  112.     printf("%d\n",head->broj);
  113.     return 0;
  114. }
  115. struct list* FindNode(struct list* head, int broj){
  116.     if(head == NULL)
  117.         return NULL;
  118.  
  119.     if(head->next == NULL)
  120.         return NULL;
  121.  
  122.     head = head->next;
  123.  
  124.     while(head->next != NULL && head->broj != broj)
  125.         head = head->next;
  126.  
  127.     return head;
  128. }
  129. int DeleteNode(struct list* head,int broj){
  130.     struct list* temp;
  131.  
  132.     if(head == NULL)
  133.         return -1;
  134.  
  135.     head  = FindPrevNode(head,broj);
  136.     if(head  == NULL || head->next == NULL)
  137.         return -1;
  138.  
  139.     temp = head->next;
  140.     head->next = temp->next;
  141.     free(temp);
  142.  
  143.     return 0;
  144. }
  145.  
  146. struct list* FindPrevNode(struct list* head, int broj){
  147.     if(head == NULL)
  148.         return NULL;
  149.  
  150.     if(head->next == NULL)
  151.         return NULL;
  152.  
  153.     head = head->next;
  154.  
  155.     while(head->next != NULL && head->next->broj != broj)
  156.         head = head->next;
  157.  
  158.     return head;
  159. }
  160.  
  161. int InsertBefore(struct list* head,int nodeBroj,int newBroj){
  162.     struct list* temp;
  163.  
  164.     if(head == NULL)
  165.         return -1;
  166.  
  167.     temp = FindPrevNode(head,nodeBroj);
  168.     InsertAfter(temp,newBroj);
  169.  
  170.     return 0;
  171. }
  172.  
  173. int FreeAlloData(struct list* head){
  174.     struct list* start;
  175.  
  176.     if(head == NULL)
  177.         return -1;
  178.  
  179.     start = head;
  180.     while(start->next != NULL){
  181.         while(head->next != NULL){
  182.             head = head->next;
  183.         }
  184.         free(head);
  185.         head= start;
  186.     }
  187.     return 0;
  188. }
  189.  
  190. int SortedInput(struct list* head,int node){
  191.     if(head == NULL)
  192.         return -1;
  193.  
  194.  
  195.     while(head->next != NULL && head->next->broj < node){
  196.         head = head->next;
  197.     }
  198.  
  199.     InsertAfter(head,node);
  200.     return 0;
  201. }
  202.  
  203. int RandomFillList(struct list* head){
  204.     int i =0;
  205.     int x =0;
  206.     if(head == NULL)
  207.         return -1;
  208.  
  209.     for(i = 0;i<BROJ_RANNDOM_BROJEVA;i++){
  210.         x = rand()%(MAX_RAND+1-MIN_RAND+MIN_RAND);
  211.         SortedInput(head,x);
  212.     }
  213.     return 0;
  214. }
  215.  
  216. int Unija(struct list* list1,struct list* list2, struct list* unija){
  217.     int flag = 0;
  218.     if(list1 == NULL || list2 == NULL || unija == NULL)
  219.         return -1;
  220.  
  221.     unija->next = NULL;
  222.         while(list1->next != NULL){
  223.             list1 = list1->next;
  224.             flag = DoesNodeExist(unija,list1->broj);
  225.             if(flag == 0)
  226.                 SortedInput(unija,list1->broj);
  227.             flag = 1;
  228.         }
  229.  
  230.         while(list2->next != NULL){
  231.             list2 = list2->next;
  232.             flag = DoesNodeExist(unija,list2->broj);
  233.             if(flag == 0)
  234.                 SortedInput(unija,list2->broj);
  235.             flag = 1;
  236.         }
  237.  
  238.  
  239.     return 0;
  240.  
  241. }
  242. int DoesNodeExist(struct list* head,int node){
  243.     if(head == NULL)
  244.         return -1;
  245.     if(head->next == NULL)
  246.         return 0;
  247.  
  248.     head = head->next;
  249.  
  250.     while(head->next != NULL){
  251.         head = head->next;
  252.         if(head->broj == node)
  253.             return 1;
  254.     }
  255.  
  256.         return 0;
  257. }
  258. int Presjek(struct list* list1,struct list* list2, struct list* presjek){
  259.     int flagL = 0;
  260.     int flagP = 0;
  261.     if(list1 == NULL || list2 == NULL || presjek == NULL)
  262.         return -1;
  263.     presjek->next = NULL;
  264.     while(list1->next != NULL){
  265.         list1 = list1->next;
  266.         flagL = DoesNodeExist(list2,list1->broj);
  267.         flagP = DoesNodeExist(presjek,list1->broj);
  268.         if(flagL == 1){
  269.             if(flagP == 0)
  270.             SortedInput(presjek,list1->broj);
  271.         }
  272.     }
  273.  
  274.     return 0;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement