Advertisement
Patresss

Untitled

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