Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5.  
  6. struct element
  7. {
  8. int i;
  9. struct element *next;
  10. };
  11.  
  12. struct element *utworz();
  13. struct element pisz(struct element *e1);
  14. struct element wyczysc(struct element *lista);
  15. struct element *dodaj (struct element *lista, int a);
  16.  
  17. struct element * dodajw(struct element *Lista,struct element *elem, int a);
  18. struct element * znajdzp(struct element *Lista, int a);
  19. struct element * usun(struct element *Lista,int a);
  20. int main()
  21. {
  22. int a,b,c,x;
  23. struct element *pp=utworz(), *elem;
  24. printf("Podaj ilosc elementow listy:");
  25. scanf("%i", &a);
  26. printf("Podaj elementy: ");
  27. for(int i = 0; i<a;i++)
  28. {
  29. scanf("%i", &x);
  30. dodaj_k(pp,x);
  31. }
  32. printf("\n",pisz(pp->next));
  33.  
  34. printf("co cie boli (i chcesz usunac):\n");
  35. scanf("%i", &a);
  36. usun(pp,a);
  37. pisz(pp->next);
  38. printf("\n co cie nie boli (i chcesz dodac):\n");
  39. scanf("%i", &b);
  40. printf ("za jakim elementem: ");
  41. scanf("%i", &c);
  42. elem=znajdzp(pp,c);
  43. dodajw(pp, elem, b);
  44. pisz(pp->next);
  45.  
  46.  
  47. return 0;
  48. }
  49.  
  50. struct element *utworz()
  51. {
  52. struct element *wsk=malloc(sizeof(struct element));
  53. wsk -> next = NULL;
  54. return wsk;
  55. };
  56.  
  57. void wyszysc (struct element *lista )
  58. {
  59. struct element *wsk=lista->next;
  60. lista=wsk;
  61.  
  62. while(lista!=NULL)
  63. {
  64. lista=lista->next;
  65. free(wsk);
  66. wsk=lista;
  67. }
  68. }
  69.  
  70. struct element pisz(struct element *e1)
  71. {
  72.  
  73. while(e1!=NULL)
  74. {
  75. printf("%i ",e1->i);
  76. e1=e1->next;
  77. }
  78. };
  79.  
  80. void dodaj_k(struct element *lista, int a)
  81. {
  82. while(lista->next!=NULL)
  83. {
  84.  
  85. lista =lista->next;
  86. }
  87. struct element *wsk = malloc (sizeof(struct element ));
  88. lista -> next = wsk;
  89. wsk -> i=a;
  90. wsk -> next = NULL;
  91. };
  92.  
  93.  
  94. struct element *dodaj (struct element *lista, int a)
  95. {
  96. struct element *wsk = malloc (sizeof(struct element));
  97. wsk -> i=a;
  98. wsk -> next=lista;
  99. return wsk;
  100. };
  101.  
  102. struct element * usun(struct element *Lista,int a){
  103. struct element *wsk,*wsk2;
  104. if(Lista==NULL)
  105. return Lista;
  106. wsk=Lista;
  107. if(Lista->i==a){
  108. Lista = Lista->next;
  109. free(wsk);
  110. }
  111. else{
  112. while((wsk->next!=NULL)&&(wsk->next->i!=a)) wsk=wsk->next;
  113.  
  114. if(wsk->next!=NULL){
  115. wsk2=wsk->next;
  116. wsk->next=wsk2->next;
  117. free(wsk2);
  118. }
  119. }
  120. return Lista;
  121. }
  122. void dodaj_p(struct element *lista, int a)
  123. {
  124. struct element *wsk = malloc(sizeof(struct element));
  125. wsk->i=a;
  126. wsk->next=lista->next;
  127. lista->next=wsk;
  128. }
  129.  
  130. struct element * dodajw(struct element *Lista,struct element *elem, int a){
  131. struct element *wsk;
  132. wsk=malloc(sizeof(struct element));
  133. wsk->i=a;
  134. if(elem==NULL){
  135. wsk->next=Lista;
  136. Lista=wsk;
  137. }
  138. else
  139. {
  140. wsk->next=elem->next;
  141. elem ->next=wsk;
  142. }
  143. return Lista;
  144. }
  145.  
  146. struct element * znajdzp (struct element *lista, int a)
  147. {
  148. lista=lista->next;
  149. while((lista->next!=NULL)&&(lista->next->i!=a))
  150. lista=lista->next;
  151. return lista;
  152. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement