Advertisement
Patresss

Untitled

Sep 2nd, 2014
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct lista {
  5. int value;
  6. struct lista * next;
  7. };
  8.  
  9. struct lista *dodaj_na_poczatek (struct lista *head,int wartosc);
  10. struct lista *dodaj_na_koniec (struct lista *head,int wartosc);
  11. struct lista *dodaj_do_posortowanej (struct lista *head,int wartosc);
  12. void szukaj_liczby (struct lista *head,int wartosc);
  13. void usun_liczbe (struct lista *head,int wartosc);
  14. struct lista *odwroc(struct lista *head);
  15.  
  16. void wypisz(struct lista *head);
  17.  
  18. int main(void)
  19. {
  20.     int szukaj,usun;
  21.         struct lista *head=NULL;
  22.  
  23. /*
  24.         head=dodaj_na_poczatek(head,1);
  25.         head=dodaj_na_poczatek(head,3);
  26.         head=dodaj_na_poczatek(head,5);
  27.         head=dodaj_na_poczatek(head,7);
  28.         head=dodaj_na_poczatek(head,9);
  29. */
  30.  
  31.  
  32.         head=dodaj_na_koniec(head,2);
  33.         head=dodaj_na_koniec(head,4);
  34.         head=dodaj_na_koniec(head,6);
  35.         head=dodaj_na_koniec(head,10);
  36.         head=dodaj_na_koniec(head,12);
  37.     //wypisz(head);
  38. /*
  39.         head=dodaj_do_posortowanej(head,23);
  40.         head=dodaj_do_posortowanej(head,13);
  41.         head=dodaj_do_posortowanej(head,67);
  42.         head=dodaj_do_posortowanej(head,3);*/
  43.         //head=dodaj_do_posortowanej(head,8);
  44.  
  45.         wypisz(head);
  46. //printf("Podaj liczbe ktora chcesz poszukacz: ");
  47. //scanf("%d", &szukaj);
  48. //szukaj_liczby(head,szukaj);
  49. //printf("Podaj liczbe ktora chcesz usunac: ");
  50. //scanf("%d", &usun);
  51. head=odwroc(head);
  52.         wypisz(head);
  53.         return 0;
  54. }
  55. /////////////////////////////////////////////////////////
  56. struct lista *dodaj_na_poczatek (struct lista *head,int wartosc)
  57. {
  58. struct lista *nowy;
  59. nowy=malloc(sizeof(struct lista));
  60.  
  61. if (nowy==NULL)
  62. {
  63.     printf("Blad\n");
  64.     return NULL;
  65. }
  66.     nowy->value=wartosc;
  67.     nowy->next=head;
  68.     return nowy;
  69.  
  70. }
  71. ///////////////////////////////////////////////////
  72. struct lista *dodaj_na_koniec (struct lista *head,int wartosc)
  73. {
  74. struct lista *nowy;
  75.     nowy=(struct lista*)malloc(sizeof(struct lista));
  76.         if (nowy==NULL)
  77. {
  78.     printf("Blad\n");
  79.     return NULL;
  80. }
  81.     nowy->value=wartosc;
  82.     nowy->next=NULL;
  83.  
  84. if (head==NULL)
  85.     head=nowy;
  86. else
  87. {
  88.     struct lista *wsk=head;
  89. while(wsk->next != NULL)
  90.     {
  91.     wsk=wsk->next;
  92.     }
  93.     wsk->next=nowy;
  94. }
  95.     return head;
  96. }
  97. ///////////////////////////////////////////////////
  98. struct lista *dodaj_do_posortowanej (struct lista *head,int wartosc)
  99. {
  100. struct lista *nowy;
  101.     nowy=(struct lista*)malloc(sizeof(struct lista));
  102.         if (nowy==NULL)
  103. {
  104.     printf("Blad\n");
  105.     return NULL;
  106. }
  107.     nowy->value=wartosc;
  108.     nowy->next=NULL;
  109.  
  110. if (head==NULL)
  111.     head=nowy;
  112. else
  113. {
  114.     struct lista *wsk=head;
  115.     if(wsk->value >= nowy->value)
  116.     {
  117.     nowy->next=head;
  118.     head=nowy;
  119.     }
  120.     else
  121. while(wsk->next!= NULL)
  122.     {
  123.     if(wsk->value <= nowy->value && wsk->next->value >= nowy->value)
  124.         {
  125.             nowy->next=wsk->next;
  126.              wsk->next=nowy;
  127.              break;
  128.         }
  129.             wsk=wsk->next;
  130.     }
  131.     if(wsk->next==NULL)
  132.         wsk->next=nowy;
  133. }
  134.     return head;
  135. }
  136. ///////////////////////////////////////////////////
  137. void szukaj_liczby (struct lista *head,int wartosc)
  138. {
  139.     struct lista *wsk=head;
  140.     int jest=0;
  141.     while (wsk->next!=NULL && wsk->value!=wartosc)
  142.         wsk=wsk->next;
  143.         if(wsk->value==wartosc)
  144.     printf("\n[szukaj] Znalazlo liczbe %d w liscie", wartosc);
  145. else printf("\n[szukaj] Nie znalazlo liczby %d w liscie", wartosc);
  146.  
  147. }
  148. ///////////////////////////////////////////////////
  149. void usun_liczbe (struct lista *head,int wartosc)
  150. {
  151.     struct lista *wsk=head;
  152.     int jest=0;
  153.     while (wsk->next!=NULL)
  154.     {
  155.         if(wsk->next->value==wartosc)
  156.         {
  157.     printf("\n[szukaj] Znalazlo liczbe %d w liscie. \n[usuwanie] Trwa usuwanie\n", wartosc);
  158.     struct lista *usuwany;
  159.     usuwany=wsk->next;
  160.     wsk->next=usuwany->next;
  161.     free(usuwany);
  162.     jest=1;
  163.         }
  164.         else
  165.             wsk=wsk->next;
  166.     }
  167. if(jest==0) printf("\n[szukaj] Nie znalazlo liczby %d w liscie\n", wartosc);
  168. }///////////////////////////////////////////////////
  169. struct lista *odwroc(struct lista *head)
  170. {
  171.  
  172.         struct lista *pocz,*wsk;
  173. pocz=NULL;
  174. wsk=head;
  175.         while(wsk->next)
  176.         {
  177.  
  178.             pocz=wsk->next;
  179.             wsk->next=pocz->next;
  180.             pocz->next=head;
  181.             head=pocz;
  182.         }
  183.         return head;
  184.     }
  185. ///////////////////////////////////////////////////
  186. void wypisz(struct lista *head)
  187. {
  188.     struct lista *wsk=head;
  189.     while (wsk!=NULL){
  190.         printf("%d \n", wsk->value);
  191.         wsk=wsk->next;
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement