Bonjour a tous!
Ma fonction reçoit une liste chaînée avec des cellules contenant des nombres contenant qu'un chiffre ou deux.Le but de ma fonction est d'afficher un nombre a deux chiffres par intermittence avec un nombre a un chiffre.
Exemple:je recois : 1->2->3->12->22->56
je veux :12->1->22->2->56->3
J'ai passe beaucoup de temps sur ce code,je ne vois pas l'erreur,l'ordi affiche rien.Votre aide m'est précieuse:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node{
int num;
struct node *next;
}Node;
Node *build_list(Node *head,int num)
{
Node *p,*q;
p=(Node *)malloc(sizeof(Node));
if(p==0)
{
exit(1);
}
p->num=num;
p->next=0;
if(head==0)
{
head=p;
}
else
{
for(q=head;q->next!=0;q=q->next);
q->next=p;
}
return head;
}
Node *duplicator(Node *head)
{
Node *p=0,*q=0,*h=0;
Node *adress=0;
for(p=head;p!=0;p=p->next)
{
if(p->num>=10)
{
q=build_list(q,p->num);
}
}
for(p=head;p!=0;p=p->next)
{
if(p->num<=9)
{
h=build_list(h,p->num);
}
}
for(;q!=0;q=q->next,h=h->next)
{
h->next= q->next;
q->next = h;
}
return q;
}
int main()
{
Node *str=0,*p=0,*q=0;
q=build_list(q,10);
q=build_list(q,11);
q=build_list(q,12);
q=build_list(q,7);
q=build_list(q,8);
q=build_list(q,9);
str=duplicator(q);
for(p=str;p!=0;p=p->next)
{
printf("%d",p->num);
}
return 0;
}