Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct node
  5. {
  6. int dane;
  7. struct node *next;
  8. };
  9.  
  10. void wypisywanie(struct node *n)
  11. {
  12. if(n == NULL){
  13. puts("Lista jest pusta");
  14. return;
  15. }
  16.  
  17. while(n != NULL){
  18. printf("%d ", n->dane);
  19. n = n->next;
  20. }
  21. }
  22.  
  23. void dodawanie_poczatek(struct node **n, int liczba)
  24. {
  25. struct node *temp = (struct node*)malloc(sizeof(struct node));
  26.  
  27. temp->dane = liczba;
  28. temp->next = *n;
  29.  
  30. *n = temp;
  31. }
  32.  
  33. void dodawanie_koniec(struct node **n, int liczba)
  34. {
  35. struct node *temp = (struct node*)malloc(sizeof(struct node));
  36. struct node *last = *n;
  37.  
  38. temp->dane = liczba;
  39. temp->next = NULL;
  40.  
  41. if(*n == NULL){
  42. *n = temp;
  43. return;
  44. }
  45.  
  46. while(last->next != NULL){
  47. last = last->next;
  48. }
  49.  
  50. last->next = temp;
  51. }
  52.  
  53. void usuwanie_poczatek(struct node **n)
  54. {
  55. struct node *temp = *n;
  56.  
  57. if(*n == NULL){
  58. puts("Lista jest pusta");
  59. return;
  60. }
  61.  
  62. *n = (*n)->next;
  63. free(temp);
  64. }
  65.  
  66. void usuwanie_koniec(struct node **n)
  67. {
  68. struct node *ostatni, *przedostatni;
  69.  
  70. if(*n == NULL){
  71. puts("Lista jest pusta");
  72. return;
  73. }
  74.  
  75. ostatni = *n;
  76. przedostatni = *n;
  77.  
  78. while(ostatni->next != NULL){
  79. przedostatni = ostatni;
  80. ostatni = ostatni->next;
  81. }
  82.  
  83. if(ostatni == *n){
  84. *n = NULL;
  85. return;
  86. }
  87.  
  88. przedostatni->next = NULL;
  89. free(ostatni);
  90. }
  91.  
  92.  
  93. int main()
  94. {
  95. struct node* pierwszy = NULL;
  96.  
  97. dodawanie_poczatek(&pierwszy, 4);
  98. dodawanie_koniec(&pierwszy, 00);
  99. dodawanie_koniec(&pierwszy, 11);
  100. dodawanie_poczatek(&pierwszy, 6);
  101. dodawanie_poczatek(&pierwszy, 1);
  102. usuwanie_poczatek(&pierwszy);
  103. usuwanie_koniec(&pierwszy);
  104. usuwanie_koniec(&pierwszy);
  105. wypisywanie(pierwszy);
  106.  
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement