Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct lista
  4. {
  5.     int dana;
  6.     struct lista* nastepny;
  7.     struct lista* poprzedni;
  8. } lista;
  9. lista* head;
  10. lista* DodajWezel(int x)
  11. {
  12.     lista* nowy=(lista*)malloc(sizeof(lista));
  13.     nowy->dana=x;
  14.     nowy->nastepny=NULL;
  15.     nowy->poprzedni=NULL;
  16.     return nowy;
  17. }
  18. void DodajElementNaPoczatku(int x)
  19. {
  20.     lista* element=DodajWezel(x);
  21.     if(head==NULL)
  22.     {
  23.         head=element;
  24.         return;
  25.     }
  26.     head->poprzedni=element;
  27.     element->nastepny=head;
  28.     head=element;
  29. }
  30. void DodajElementNaKoncu(int x)
  31. {
  32.     lista* element=DodajWezel(x);
  33.     lista* temp= head;
  34.     if(head==NULL)
  35.     {
  36.         head=element;
  37.         return;
  38.     }
  39.     while(temp->nastepny!=NULL)
  40.     {
  41.         temp=temp->nastepny;
  42.     }
  43.     temp->nastepny=element;
  44.     element->poprzedni=temp;
  45. }
  46. void WyswietlListe()
  47. {
  48.     lista*temp=head;
  49.     if(head==NULL)
  50.     {
  51.         printf("Lista pusta\n");
  52.         exit(1);
  53.     }
  54.     printf("\nPoczatek listy\n");
  55.     while(temp!=NULL)
  56.     {
  57.         printf("%d \n", temp->dana);
  58.         temp=temp->nastepny;
  59.     }
  60.     printf("\nKoniec listy\n");
  61. }
  62. int DlugoscListy()
  63. {
  64.     lista*temp=head;
  65.     if(head==NULL)
  66.     {
  67.         printf("Lista pusta\n");
  68.         exit(1);
  69.     }
  70.     int i=0;
  71.     while(temp!=NULL)
  72.     {
  73.         temp=temp->nastepny;
  74.         i++;
  75.     }
  76.     return i;
  77. }
  78. lista** ListaNaTablice(int dlugosc)
  79. {
  80.     lista* temp=head;
  81.     lista** tab;
  82.     tab=(lista**)malloc(dlugosc*sizeof(lista*));
  83.     if(tab==NULL)
  84.     {
  85.         printf("Blad malloca \n");
  86.         exit(1);
  87.     }
  88.     int i=0;
  89.     while(temp!=NULL)
  90.     {
  91.         tab[i]=temp;
  92.         temp=temp->nastepny;
  93.         i++;
  94.     }
  95.     return tab;
  96. }
  97.  
  98. int porownaj(const void* first, const void* second)
  99. {
  100.     lista* a = *(lista**)first;
  101.     lista* b = *(lista**)second;
  102.     if((a->dana)>(b->dana)) return 1;
  103.     if((a->dana)==(b->dana)) return 0;
  104.     else return -1;
  105. }
  106. void Sortowanie(lista** tab, int dlugosc)
  107. {
  108.     qsort(tab, dlugosc, sizeof(tab), porownaj);
  109. }
  110. void TablicaNaListe(lista** tab, int dlugosc)
  111. {
  112.     lista* obecny;
  113.     lista* kolejny;
  114.     head=tab[0];
  115.     obecny=head;
  116.     int i=1;
  117.     for(i=1;i<dlugosc;i++)
  118.     {
  119.         kolejny=tab[i];
  120.         obecny->nastepny=kolejny;
  121.         kolejny->poprzedni=obecny;
  122.         obecny=kolejny;
  123.     }
  124.     obecny->nastepny=NULL;
  125. }
  126. int main(void)
  127. {
  128.     head=NULL;
  129.     lista** tab;
  130.     int k=0;
  131.     int x=0;
  132.     printf("Podaj ilosc elementow do wprowadzenia: ");
  133.     scanf("%d", &k);
  134.     int i=0;
  135.     for(i=0;i<k;i++)
  136.     {
  137.         printf("Podaj element: ");
  138.         scanf("%d", &x);
  139.         DodajElementNaPoczatku(x);
  140.     }
  141.     int d=DlugoscListy();
  142.     printf("Lista ma %d elementow\n", d);
  143.     printf("Lista przed sortowaniem: \n");
  144.     WyswietlListe();
  145.     ListaNaTablice(d);
  146.     tab=ListaNaTablice(d);
  147.     Sortowanie(tab, d);
  148.     TablicaNaListe(tab, d);
  149.     printf("Lista po sortowaniu: \n");
  150.     WyswietlListe();
  151.     free(tab);
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement