Z gory dziekuje:)
Kod: Zaznacz cały
#include <stdio.h>
#include <stdlib.h>
struct lista {
int key;
struct lista *next;
} *head=NULL;
void dodaj(int ile){
struct lista *part=NULL, *ppart=head;
int liczba;
printf("Podaj 1 liczbe: ");
scanf("%d",&liczba);
head->key=liczba;
for(int i=1;i<ile;i++){
printf("Podaj %d liczbe: ",i+1);
scanf("%d",&liczba);
part=(lista*)malloc(sizeof(lista));
ppart->next=part;
part->next=NULL;
part->key=liczba;
ppart=part;
}
}
void wypisz(struct lista *head){
if(head) {
printf("%d
",head->key);
wypisz(head->next);}
}
void sortowanie(struct lista *head){
lista *pom, *pom_pop;
lista * pop=NULL;
lista * nast;
bool ctrl=1;
while (ctrl=1)
{
printf("element do wfeiwubgr:");
pom_pop=NULL;
pom=head;
ctrl=0;
while(pom->next != NULL)
{
if (pom->key > pom->next->key)
{
ctrl=1;
if (pom_pop)
{
pom_pop->next = pom->next;
pom->next = pom->next->next;
pom_pop->next->next = pom;
}
else
{
head = head->next;
pom->next = pom->next->next;
head->next = pom;
}
}
pom_pop=pom;
pom=pom->next;
}
}
}
int main(void){
head=(lista*)malloc(sizeof(lista));
head->next=NULL;
int il;
printf("Ile elementow? ");
scanf("%d",&il);
dodaj(il);
wypisz(head);
sortowanie(head);
wypisz(head);
getchar();
return 0;
}