Guest User

Vezane liste

a guest
Aug 20th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<stdio.h>
  5.  
  6. typedef struct broj * br;
  7.  
  8. struct broj
  9. {
  10.     int b;
  11.     br next;
  12. };
  13.  
  14. void dodaj1(br p1)
  15. {
  16.     printf("dodaj elemente u prvu listu\n");
  17.     br temp;
  18.     temp=(br)malloc(sizeof(struct broj));
  19.     printf("Unesite broj :");
  20.     scanf("%d",&temp->b );
  21.     temp->next=p1->next;
  22.     p1->next=temp;
  23. }
  24.  
  25. void dodaj2(br p2)
  26. {
  27.     printf("dodaj elemente u drugu listu\n");
  28.     br temp;
  29.     temp=(br)malloc(sizeof(struct broj));
  30.     printf("Unesite broj :");
  31.     scanf("%d",&temp->b );
  32.     temp->next=p2->next;
  33.     p2->next=temp;
  34. }
  35.  
  36. void ispis1(br p1)
  37. {
  38.     while(p1!=NULL)
  39.     {
  40.         printf("%d \n",p1->b);
  41.         p1=p1->next;
  42.     }
  43. }
  44.  
  45. void ispis2(br p2)
  46. {
  47.     while(p2!=NULL)
  48.     {
  49.         printf("%d \n",p2->b);
  50.         p2=p2->next;
  51.     }
  52. }
  53.  
  54. void presjek(br p1,br p2,br p)
  55. {
  56.     br temp;
  57.     while(p1!=NULL && p2!=NULL)
  58.     {
  59.         if(p1->b == p2->b) 
  60.         {
  61.             temp=(br)malloc(sizeof(struct broj));
  62.             temp->b=p1->b;
  63.             temp->next=p->next;
  64.             p->next=temp;
  65.             p1=p1->next;
  66.             p2=p2->next;
  67.             p=p->next;
  68.         }
  69.         else if(p2->b < p1->b)
  70.         {
  71.             p2=p2->next;
  72.         }
  73.         else
  74.         {
  75.             p1=p1->next;
  76.         }
  77.     }
  78. }
  79.  
  80. void ispisp(br p)
  81. {
  82.     printf("\n");
  83.     p = p->next;
  84.     while(p != NULL)
  85.     {
  86.         printf("\t%d", p->b);
  87.         p = p->next;
  88.     }
  89.     printf("\n");
  90. }
  91.  
  92. int main()
  93. {
  94.  
  95.     struct broj p1;
  96.     struct broj p2;
  97.     p1.next=NULL;
  98.     p2.next=NULL;
  99.     struct broj p;
  100.     p.next=NULL;
  101.     char izbor=0;
  102.     while(izbor!='k')
  103.     {
  104.         printf("unesite A za dodaj u prvu,B za dodaj u drugu,C za ispis prve,D za ispis druge,E za uniju:\n");
  105.         scanf(" %c", &izbor);
  106.         switch(izbor)
  107.  
  108.         {
  109.             case 'a':
  110.             dodaj1(&p1);
  111.             break;
  112.  
  113.             case 'b':
  114.             dodaj2(&p2);
  115.             break;
  116.  
  117.             case 'c':
  118.             ispis1(p1.next);
  119.             break;
  120.  
  121.             case 'd':
  122.             ispis2(p2.next);
  123.             break;
  124.  
  125.             case 'e':
  126.             presjek(&p1,&p2,&p);
  127.             break;
  128.  
  129.             case 'f':
  130.             ispisp(p.next);
  131.  
  132.             break;
  133.  
  134.             case 'k':
  135.             printf("Kraj");
  136.             break;
  137.  
  138.             default:
  139.             printf("pogresan odabir\n");
  140.  
  141.         }
  142.     }
  143.     system("PAUSE");
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment