Пожалуйста помогите сделать задачку! в СИ
Дан список чисел, поделить его на два списка по четности, в четном списке отсортировать числа по убыванию, в нечетном по возрастанию.На экран вывести исходный список и два получившихся.
P.S. Есть задачка с такой же сортировкой, но она делает это в одном списке.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct item
{int a;
struct item *next;
};
struct item *form()//formirovanie spiska
{
int n;
struct item *head=NULL, *cur, *tmp;
do
{
printf("Input n from 0 to 100\n");
scanf("%d",&n);
printf("Input spisok\n");
} while (n<0&& n>100);
while (n-->0)
{
tmp=malloc(sizeof(struct item));
scanf("%d",&tmp->a);
tmp->next=NULL;
if (!head)
head=tmp;
else cur->next=tmp;
cur=tmp;
}
return head;
}
void print(struct item *p)
{
for (;p;p=p->next)
printf("%d",p->a);
printf("\n");
}
void del(struct item *p)
{
struct item *tmp;
while (p)
{
tmp=p;
p=p->next;
free(tmp);
}
}
struct item *work(struct item *p){
struct item *head=NULL, *cur,*tmp,*q,*g;
int k_chet=0, k_nech=0,temp,i,j;
for (q=p;q;q=q->next)
if (!(q->a % 2)){
tmp=malloc(sizeof(struct item));
tmp->a=q->a;
tmp->next=NULL;
if (head)
cur->next=tmp;
else
head=tmp;
cur=tmp;
k_chet++;
}
for (q=p;q;q=q->next)
if (q->a % 2){
tmp=malloc(sizeof(struct item));
tmp->a=q->a;
tmp->next=NULL;
if (head)
cur->next=tmp;
else
head=tmp;
cur=tmp;
k_nech++;
}
for (i=1;i<100;i++)
{
j=1;
for (q=head;j<k_chet;j++,q=q->next)
if(q->a <q->next -> a){
temp=q->a;
q->a=q->next->a;
q->next->a=temp;
}
}
printf("\n");
for (i=1;i<100;i++)
{
j=0;
for (q=head;j<k_chet;j++,q=q->next);
for (;j<k_nech;j++,q=q->next)
if (q->a>q->next->a){
temp=q->a;
printf("%d\n",temp);
q->a=q->next->a;
q->next->a=temp;
}
}
return head;
}
int main()
{
struct item *ish,*rez;
ish=form();
print(ish);
rez=work(ish);
print(rez);
del(rez);
del(ish);
return 0;
}