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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<malloc.h>
#include <stdlib.h>
typedef struct node
{
/* donnees */
int x;
char nom_prenom[20];
/* chainage */
struct node *p_next;
}nodes;
//fonction d'additition
struct node *add_end (struct node *p_head, char v[20])
{
/* allocation du noeud */
struct node *p_new = malloc (sizeof *p_new);
/* si tout s'est bien passe : */
if (p_new != NULL)
{
/* mise a jour des champs : */
/* donnees */
p_new->x = 10;
strcpy(p_new->nom_prenom , v);
/* chainage par defaut */
p_new->p_next = NULL;
/* chainage */
if (p_head == NULL)
{
/* c'est le premier : */
p_head = p_new;
}
else
{
/* on cherche le dernier noeud */
struct node *p = p_head;
while (p->p_next != NULL)
{
/* pointer sur le suivant */
p = p->p_next;
}
/* modification du chainage */
p->p_next = p_new;
}
}
return p_head;
}
//fonction de print
void display (struct node *p_head)
{
struct node *p = p_head;
while (p != NULL)
{
/* afficher les données courantes */
printf ("%s --> %d \n",p->nom_prenom ,p->x);
/* pointer sur le suivant */
p = p->p_next;
}
/* afficher la fin */
printf ("NULL\n");
}
//fonction delete;
int dellete(struct node*p_head,char v[20])
{ int pos;
struct node*p=p_head;
struct node*sav;
struct node*tmp;
while ( p->p_next!=NULL && !strcmp(p->nom_prenom,v)!=1)
{
p=p->p_next;
tmp=p;
}
sav = p->p_next;
free(p->p_next);
p->p_next = sav;
}
//fonction principale
int main (void)
{int l=0;
char i[20];
struct node *p_head = NULL;
while( l <3 )
{ printf("entre element ");
scanf("%s",&i);
p_head = add_end (p_head, i);
l++;}
printf("le nom_prenom-->nombred'abcence\n");
display (p_head);
dellete(p_head ,"aziz");
display (p_head);
getch();
return 0;
} |
Partager