Advertisement
HappyButter

lista cykl. - usuwanie parzystych

Jun 8th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct tnode
  5. {
  6.     int value;
  7.     struct tnode *next;
  8. } tnode;
  9.  
  10.  
  11. tnode *add(tnode *list_1, tnode *list_2)
  12. {
  13.     tnode *hold=list_1;
  14.     if (hold==NULL)
  15.     {
  16.         list_2->next=list_2;
  17.         return list_2;
  18.     }
  19.     else
  20.     {
  21.         tnode *current_1=list_1;
  22.         while(current_1->next!=hold)
  23.         {
  24.             current_1=current_1->next;
  25.         }
  26.  
  27.         current_1->next=list_2;
  28.         list_2->next=hold;
  29.  
  30.         return hold;
  31.     }
  32. }
  33.  
  34. void wypisz_cycle ( tnode *list)
  35. {
  36.     if (list == NULL)
  37.     {
  38.         printf ("PUSTA \n");
  39.         return;
  40.     }
  41.     tnode * wsk = list;
  42.     do
  43.     {
  44.         printf ("[%d]  ", list->value);
  45.         list = list->next;
  46.     }
  47.     while (list != wsk);
  48.     return;
  49. }
  50.  
  51. void wypisz(tnode *list)
  52. {
  53.     if (list==NULL)
  54.         return;
  55.     while (list!=NULL)
  56.     {
  57.         printf("[%d]->",list->value);
  58.         list=list->next;
  59.     }
  60.     puts("\n");
  61. }
  62.  
  63. void usun(tnode **list)
  64. {
  65.     if ((*list)==NULL)
  66.         return;
  67.     tnode *head=*list;
  68.     tnode *last=*list;
  69.     while (last->next!=head)
  70.         last=last->next;
  71.     last->next=NULL;
  72.  
  73.     while ((head->value)%2==0 && head!=NULL)
  74.     {
  75.         tnode *tmp=head;
  76.         head=head->next;
  77.         free(tmp);
  78.     }
  79.     *list=head;
  80.  
  81.     if(head==NULL)
  82.     {
  83.         return;
  84.     }
  85.  
  86.     tnode *current=*list;
  87.     tnode *prev=*list;
  88.     current=current->next;
  89.     while(current!=NULL)
  90.     {
  91.         if((current->value)%2==0)
  92.         {
  93.             current=current->next;
  94.             free(prev->next);
  95.             prev->next=current;
  96.         }
  97.         else
  98.         {
  99.             current=current->next;
  100.             prev=prev->next;
  101.         }
  102.     }
  103.  
  104. }
  105.  
  106. int main()
  107. {
  108.     tnode *head=NULL;
  109.     tnode *one=malloc(sizeof(tnode));
  110.     tnode *two=malloc(sizeof(tnode));
  111.     tnode *three=malloc(sizeof(tnode));
  112.     tnode *four=malloc(sizeof(tnode));
  113.     one->value=2;
  114.     one->next=NULL;
  115.     two->value=3;
  116.     two->next=NULL;
  117.     three->value=4;
  118.     three->next=NULL;
  119.     four->value=5;
  120.     four->next=NULL;
  121.  
  122.     head=add(head,one);
  123.     head=add(head,two);
  124.     head=add(head,three);
  125.     head=add(head,four);
  126.     puts("Pierwsza lista:");
  127.     wypisz_cycle(head);
  128.     printf("lista1 = %d, lista2 = %d\n",head->value,head->next->value);
  129.     usun(&head);
  130.     //wypisz_cycle(head);
  131.     wypisz(head);
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement