Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. struct lista {
  6.     int val;
  7.     struct lista *next;
  8. };
  9.  
  10.  
  11. struct lista *load(struct lista *head);
  12. struct lista *intersection(struct lista *head1, struct lista *head2);
  13. struct lista *deall(struct lista *head);
  14. void print(struct lista *head);
  15.  
  16.  
  17. int main()
  18. {
  19.     int v;
  20.     struct lista *p1, *p2, *p3;
  21.        
  22.     p1=load(p1);
  23.     p2=load(p2);
  24.    
  25.     p3=intersection(p1,p2);
  26.  
  27.     print(p3);
  28.    
  29.     return 0;
  30. }
  31.  
  32.  
  33. struct lista *load(struct lista *head)
  34. {
  35.     int v=1;
  36.     struct lista *punt, *newEl;
  37.    
  38.     head=malloc(sizeof(struct lista));
  39.     head->next=NULL;
  40.     punt=head;
  41.    
  42.     bool once = true;
  43.    
  44.     while(v>=0)
  45.     {
  46.         scanf("%d", &v);
  47.        
  48.         if(once == true && v>=0)
  49.         {
  50.             punt->val = v;
  51.             once = false;
  52.             continue;
  53.         }
  54.        
  55.         if(v<0)
  56.             return head;
  57.        
  58.         newEl=malloc(sizeof(struct lista));
  59.         newEl->val=v;
  60.         punt->next=newEl;;
  61.         punt=punt->next;
  62.     }
  63.    
  64.     return head;
  65. }
  66.  
  67.  
  68. struct lista *intersection(struct lista *head1, struct lista *head2)
  69. {  
  70.     struct lista *punt3, *punt2, *punt1, *newEl, *head3;
  71.     int a=1, b, c=0;
  72.     bool once=true;
  73.     head3=malloc(sizeof(struct lista));
  74.     head3->next=NULL;
  75.    
  76.     punt3=malloc(sizeof(struct lista));
  77.    
  78.     punt2=head2;
  79.     punt1=head1;
  80.    
  81.     while(punt1!=NULL)
  82.     {
  83.         a=1;
  84.         while(punt2!=NULL && a!=0)
  85.         {
  86.             if(punt2->val==punt1->val)
  87.             {
  88.                 a=0;
  89.                 punt3=head3;
  90.                
  91.                 if(once == true)
  92.                 {
  93.                     head3->val = punt2->val;
  94.                     b=head3->val;
  95.                     once = false;
  96.                     c=1;
  97.                     break;
  98.                 }
  99.            
  100.                 if(head3->val>punt2->val && punt2->val!=b)
  101.                 {
  102.                     newEl=malloc(sizeof(struct lista));
  103.                     newEl->next=head3;
  104.                     newEl->val=punt2->val;
  105.                     head3=newEl;
  106.                 }
  107.                
  108.                 else
  109.                 {
  110.                     while(punt3->next!=NULL && punt2->val>punt3->next->val && punt2->val!=b)
  111.                         punt3=punt3->next;
  112.                    
  113.        
  114.                 if((punt3->next==NULL || (punt2->val<punt3->next->val && punt2->val>punt3->val) && c==0))
  115.                 {
  116.                
  117.                     newEl=malloc(sizeof(struct lista));
  118.                     newEl->val=punt2->val;
  119.                     newEl->next=punt3->next;
  120.                     punt3->next=newEl;
  121.                 }       c=0;
  122.                 }
  123.             }
  124.            
  125.         else
  126.             punt2=punt2->next;
  127.         }
  128.        
  129.         punt2=head2;
  130.         punt1=punt1->next;
  131.     }
  132.             if(head3->next==NULL)
  133.             head3=NULL;
  134.     return head3;
  135. }
  136.  
  137.  
  138. struct lista *deall(struct lista *head)
  139. {
  140.    
  141. }
  142.  
  143. void print(struct lista *head)
  144. {
  145.     while(head!=NULL)
  146.     {
  147.         printf("%d\n", head->val);
  148.         head=head->next;
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement