fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Liczba{
  5. int wartosc;
  6. struct Liczba *nastepny;
  7. struct Liczba *poprzedni;
  8. struct Liczba *ostatni;
  9. };
  10.  
  11. void dodaj(struct Liczba **glowa, int wartosc){
  12. struct Liczba *element = (struct Liczba*)malloc(sizeof(struct Liczba));
  13. element->wartosc = wartosc;
  14. element->nastepny = element->poprzedni = element->ostatni = NULL;
  15.  
  16. if((*glowa) == NULL){
  17. (*glowa) = element;
  18. (*glowa)->nastepny = (*glowa);
  19. (*glowa)->poprzedni = (*glowa);
  20. (*glowa)->ostatni = (*glowa);
  21. }
  22. else{
  23. (*glowa)->ostatni->nastepny = element;
  24. element->poprzedni = (*glowa)->ostatni;
  25. element->nastepny = (*glowa);
  26. (*glowa)->poprzedni = element;
  27. (*glowa)->ostatni = element;
  28. }
  29. }
  30.  
  31. void wypisz(struct Liczba *glowa){
  32. struct Liczba *temp = glowa;
  33. while(temp != glowa->ostatni){
  34. printf("%d ", temp->wartosc);
  35. temp = temp->nastepny;
  36. }
  37. printf("%d ", temp->wartosc);
  38. printf("\n");
  39. }
  40.  
  41. void sortuj(struct Liczba *glowa){
  42. struct Liczba *iter_1 = NULL;
  43. struct Liczba *iter_2 = NULL;
  44. int temp = 0;
  45.  
  46. for(iter_1 = glowa; iter_1 != glowa->ostatni; iter_1 = iter_1->nastepny){
  47. for(iter_2 = glowa; iter_2 != glowa->ostatni; iter_2 = iter_2->nastepny){
  48. if(iter_1->wartosc > iter_2->wartosc){
  49. temp = iter_1->wartosc;
  50. iter_1->wartosc = iter_2->wartosc;
  51. iter_2->wartosc = temp;
  52. }
  53. }
  54. }
  55. }
  56.  
  57.  
  58. void zniszcz(struct Liczba **glowa){
  59. struct Liczba *temp = (*glowa);
  60. struct Liczba *doZniszczenia = NULL;
  61. while(temp != (*glowa)->ostatni){
  62. doZniszczenia = temp;
  63. temp = temp->nastepny;
  64.  
  65. free(doZniszczenia);
  66. doZniszczenia = NULL;
  67. }
  68. }
  69.  
  70. int main(void)
  71. {
  72. struct Liczba *tab = NULL;
  73. dodaj(&tab, 10);
  74. dodaj(&tab, 2);
  75. dodaj(&tab, -3);
  76. dodaj(&tab, 41);
  77. dodaj(&tab, 98);
  78. dodaj(&tab, 3);
  79. dodaj(&tab, -11);
  80. printf("\nPrzed sortowaniem:\n");
  81. wypisz(tab);
  82. sortuj(tab);
  83. printf("\nPo posortowaniu:\n");
  84. wypisz(tab);
  85. zniszcz(&tab);
  86. return 0;
  87. }
  88.  
  89.  
Success #stdin #stdout 0s 2140KB
stdin
Standard input is empty
stdout
Przed sortowaniem:
10 2 -3 41 98 3 -11 

Po posortowaniu:
98 41 10 3 2 -3 -11