Advertisement
meraxes

Union of sets using linked lists

Nov 11th, 2017
3,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct node *position;
  6.  
  7. typedef struct node
  8. {
  9.     int num;
  10.     position next;
  11. }_NODE;
  12.  
  13. void ReadFromFile(position);
  14. void Print(position);
  15. void Union(position, position, position);
  16. int main(int argc, char *argv[])
  17. {
  18.  
  19.     _NODE L1,L2,PRS,UN;
  20.  
  21.     L1.next=NULL;
  22.     L2.next=NULL;
  23.     PRS.next = NULL;
  24.  
  25.     ReadFromFile(&L1);
  26.     Print(L1.next);
  27.  
  28.     ReadFromFile(&L2);
  29.     Print(L2.next);
  30.  
  31.     printf("\n");
  32.  
  33.     Union(&UN, L1.next, L2.next);
  34.     Print(UN.next);
  35.  
  36.     return 0;
  37. }
  38. void ReadFromFile(position p)
  39. {
  40.     FILE *input;
  41.     char file[50];
  42.     position tmp,z;
  43.    
  44.     printf("\nDatoteka:");
  45.     scanf(" %s", &file);
  46.  
  47.     input = fopen(file, "r");
  48.  
  49.     if(input == NULL)
  50.     {
  51.         printf("Datoteka nije otvorena.");
  52.     }
  53.     else
  54.     {
  55.         while(!feof(input))
  56.         {
  57.             z=(position) malloc(sizeof(_NODE));
  58.             if(z==NULL)
  59.                 printf("Greska u alokaciji memorije.");
  60.             else
  61.             {
  62.                 fscanf(input," %d", &z->num);
  63.  
  64.                 tmp=p;
  65.  
  66.                 while(tmp->next != NULL && tmp->next->num < z->num)
  67.                     tmp = tmp->next;
  68.  
  69.                 z->next = tmp->next;
  70.                 tmp->next = z;
  71.             }
  72.  
  73.         }
  74.         fclose(input);
  75.     }
  76.    
  77. }
  78. void Print(position p)
  79. {
  80.     while (p != NULL)
  81.     {
  82.         printf("%d\t",p->num);
  83.         p=p->next;
  84.     }
  85. }
  86. void Union(position un, position p, position q)
  87. {
  88.     position z,tmp;
  89.  
  90.     while (p != NULL && q != NULL)
  91.     {
  92.  
  93.         z = (position)malloc(sizeof(_NODE));
  94.         if (z == NULL)
  95.         {
  96.             printf("Memory allocation error!");
  97.             break;
  98.         }
  99.         else
  100.         {
  101.             if (p->num < q->num)
  102.             {
  103.                 z->num = p->num;
  104.                 z->next = un->next;
  105.                 un->next = z;
  106.  
  107.                 p = p->next;
  108.             }
  109.             else if (p->num > q->num)
  110.             {
  111.                 z->num = q->num;
  112.                 z->next = un->next;
  113.                 un->next = z;
  114.            
  115.                 q = q->next;
  116.             }
  117.             else
  118.             {
  119.                 z->num = p->num;
  120.  
  121.                 z->next = un->next;
  122.                 un->next = z;
  123.  
  124.                 p = p->next;
  125.                 q = q->next;
  126.             }
  127.             if (z->num == un->num)
  128.             {
  129.                 un->next = z->next;
  130.                 free(z);
  131.             }
  132.         }
  133.     }
  134.     if (p == NULL)
  135.     {
  136.         tmp = q;
  137.         while (tmp != NULL)
  138.         {
  139.             z = (position)malloc(sizeof(_NODE));
  140.             if (z == NULL)
  141.             {
  142.                 printf("Memory allocation error!.");
  143.                 break;
  144.             }
  145.             else
  146.             {
  147.                 z->num = tmp->num;
  148.                 z->next = un->next;
  149.                 un->next = z;
  150.                 tmp = tmp->next;
  151.             }
  152.         }
  153.     }
  154.     if (q == NULL)
  155.     {
  156.         tmp = p;
  157.         while (tmp != NULL)
  158.         {
  159.             z = (position)malloc(sizeof(_NODE));
  160.             if (z == NULL)
  161.             {
  162.                 printf("Memory allocation error!");
  163.                 break;
  164.             }
  165.             else
  166.             {
  167.                 z->num = tmp->num;
  168.                 z->next = un->next;
  169.                 un->next = z;
  170.             }
  171.             tmp = tmp->next;
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement