Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define M 3
- typedef struct lista
- {
- int id;
- struct lista *nastepny;
- }lista;
- lista* dodawanie (lista *head,int dana)
- {
- lista *nowy;
- nowy=(lista *)malloc(sizeof(lista));
- nowy->id=dana;
- nowy->nastepny=head;
- return nowy;
- }
- int porownanie (const void *a,const void *b)
- {
- const lista* el1=a;
- const lista* el2=b;
- if (el1->id > el2->id)
- {
- return 1;
- }
- else if (el1->id == el2->id)
- {
- return 0;
- }
- else return -1;
- }
- void wyswietlanie(lista *head)
- {
- lista *current = head;
- while(current->nastepny != NULL)
- {
- printf("%d\n",current->id);
- current = current->nastepny;
- }
- }
- lista * listtoarray(lista* head_t)
- {
- int i;
- struct lista *tablica;
- tablica=(lista *)malloc(M*sizeof(lista));
- for (i = 0; i < M;i++)
- {
- (tablica+i)->id=head_t->id;
- (tablica+i)->nastepny=head_t->nastepny;
- head_t = head_t->nastepny;
- }
- return tablica;
- }
- int main()
- {
- lista *head_t=malloc(sizeof(lista));
- head_t=dodawanie(head_t,3);
- head_t=dodawanie(head_t,1);
- head_t=dodawanie(head_t,2);
- lista *lel;
- lel=head_t;
- wyswietlanie(head_t);
- lista * tablica=listtoarray(head_t);
- qsort(tablica,M,sizeof(lista),porownanie);
- printf("%d\t%d\t%d\n",tablica[0].id,tablica[1].id,tablica[2].id);
- int i;
- for (i = 0; i < M; i++)
- {
- {
- tablica[i].nastepny=NULL;
- }
- tablica[i].nastepny=&tablica[i+1];
- }
- tablica[M].nastepny=NULL;
- head_t->id=tablica[0].id;
- head_t->nastepny=&tablica[1];
- wyswietlanie(head_t);
- free(head_t);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement