1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int coeff;
int pow;
struct node *next_node;
};
struct node *CreateNode(int c, int p) {
struct node *new_node=malloc(sizeof(struct node));
if(new_node) {
new_node->coeff=c;
new_node->pow=p;
new_node->next_node=NULL;
}
return(new_node);
}
struct node *GetLastNode(struct node *ptr) {
if(!ptr)
return(NULL);
struct node *last_node;
do {
last_node=ptr;
ptr=ptr->next_node;
} while(ptr);
return(last_node);
}
bool InsertNodeAtEndOfList(struct node **first_node, int c, int p) {
struct node *new_node=CreateNode(c, p); // le 1)
if(!new_node)
return(false);
struct node *last_node=GetLastNode(*first_node); // le 2)
if(!last_node)
*first_node=new_node; // liste vide (le 'then')
else
last_node->next_node=new_node; // le 3)
return(true);
}
void DisplayList(struct node *ptr) {
for(int n=1; ptr; ptr=ptr->next_node, ++n)
printf("%2d\t\%d\t%d\n", n, ptr->coeff, ptr->pow);
puts("--------------------------------");
}
void Check(struct node **first_node, int c, int p) {
if(InsertNodeAtEndOfList(first_node, c, p))
DisplayList(*first_node);
else
printf("Erreur insertion pour %d %d\n", c, p);
}
int main(void) {
struct node *first_node=NULL;
Check(&first_node, 50, 51);
Check(&first_node, 40, 41);
Check(&first_node, 10, 11);
Check(&first_node, 30, 31);
Check(&first_node, 20, 21);
return(0);
} |
Partager