Advertisement
Patresss

Untitled

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