Thread: always (debug error )----> listes

  1. #1
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159

    Angry always (debug error )----> listes


    i'm trying to have recursive functions

    to manipulate list the problem is in ajoutete(list);

    her is my code :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct cellule
    {
    int info;
    cellule *suivant;
    
    }cellule,*liste;
    
    
    
    
    
    liste ajoutfinr(liste l, int a )
    {
    
    if ( ( l == NULL ) || ( ( l->suivant ) == NULL ) )
    {
    
    liste N =  malloc ( sizeof (cellule ) );
    
    N->info = a ;
    
    
    if( l == NULL ) { return N; }
    /******** see the error here ********/
    else if ( ( l->suivant ) == NULL  ) 
    {
    
    
    l->suivant = N ;
    
    
    }
    /************* end error ***********/ 
    return l;
    
    }
    else
    {
    
    	return( ajoutfinr( l->suivant , a ) ) ;
    
    }
    
    
    }
    
    
    liste remplir_finr(liste p)
    
    {
    	
    int n,a;
    
    printf("Entrer le nombre d'elements de la liste: ");scanf("%d",&n);
    
    
    for( i=0; i<n ; i++)
    {
    
      printf("Entrer l'element %i: \n",i+1);
      
      scanf("%i",&a);
      
      p = ajoutfinr( p , a );
    
    }
    
    return(p);
    
    }
    
    
    int main()
    {
    
    /************ functions declared in another file ********/
    
    void afficher(liste tete);
    liste nouveau();
    liste suivant(liste p);
    
    /************* end  *************/
    
    
    liste l;
    
    l = nouveau( )  ;
    
    printf("saisie liste 1 --> remplir_tete\n");
    
    l = remplir_finr( l ) ;
    
    printf("affichage menu\n");
    
    afficher( l ) ;
    
    return 0;
    
    }
    Last edited by enjoy; 01-08-2005 at 09:10 AM. Reason: edit code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    http://cboard.cprogramming.com/showthread.php?t=60405
    Do you have some disease which destroys working code and indentation?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159

    i dont understend salem
    my english is not perfect
    Code:
    please ...

    it 's not a simple list but recursive list

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    English has nothing to do with your inability to indent code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    ok ...
    but what's the solution !!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    When you can be bothered to format your code, I (and others) might be bothered to look at it.

    There are a finite number of helpers
    Each helper has a finite amount of time to help people
    Helpers pick the posts which
    - are nicely formatted
    - have well presented questions

    Bottom line is - if you keep posting poorly formatted code, people will ignore you, no matter how easy your question is to answer.

    I've done talking now - I'll say no more until you learn how to present your code properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159

    thank you salem
    i have got a solution


    so

    the problem now is to delete element in list

    i have write 2 solution

    the first does not work ... !

    but the second is ok

    all functions ......> Ok
    can some one help to corerrect the first solution
    here is my

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct cellule
    {
    	int nbr;
    	cellule *suivant;
    
    }cellule;
    
    typedef cellule *liste;
    
    
    
    
    /* fin */
    
    int fin(liste l)
    {
    	liste p;
    
    p = l->suivant;
    
    while ( p != NULL)
    {
    
    p = p->suivant;
    
    l = l->suivant; 
    
    } 
    
    return(l->nbr);
    
    }
    
    
    /* new */
    liste nouveau(){ return NULL; }
    
    /* next */
    liste suivant(liste l){ return l->suivant; }
    
    /* precedent */
    
    int precedent(liste l,int elm)
    {
    	if( l == NULL ) return l->nbr;
    
    	liste p=l->suivant;
    
    while ( p != NULL)
    {
    
    if( p->nbr == elm ) break;
    
    p = p->suivant;
    
    l = l->suivant;
    
    
    } 
    return l->nbr;
    }
    
    /* ajout tete */
    liste ajout_tete(liste l,int element)
    
    {
    
    liste A = ( liste )malloc( sizeof( cellule ) );
    
    if ( A != NULL )
    {
    
    A->nbr=element;
    A->suivant=l;
    
    }
    
    return A;
    
    }
    
    /* recherche par element */
    
    liste rechercher(liste l,int element)
    {
         int e=0;
    	liste p = l;
    
    if( l == NULL ){printf("liste Null !");return l;}
    
    while( p != NULL )
    {
    
    	if( p->nbr == element ) {e=1; break; }
    
    p = suivant ( p );
    
    } 
    
    if( e == 0 ){ return NULL; }
    
    return p;
    
    
    
    }
    /* ajout fin */
    liste ajout_fin(liste l,int element)
    {
    
    liste A = ( liste )malloc( sizeof( cellule ) );
    
    A->nbr = element;
    
    A->suivant = NULL;
    
    if( l == NULL ){ return A; }
    else 
    {
    
    liste p1,p2;
    p1 = l;
    p2 = l->suivant;
    
    while( p2 != NULL )
    {
    
    p1 = suivant ( p1 ); 
    p2 = suivant ( p2 ); 
    
    } 
    	
    
    	
    
    p1->suivant = A;
    
    }
    
    return l;
    
    }
    
    /* remplir tete */
    
    liste remplir_tete(liste l)
    {
    	int  n , i , element;
    	
    	while( n <= 0 )
    	{
    printf("\n \02 Donnez nbr element a inserer par tete : ?\b ");
    scanf("%i",&n);
    	}
    
    for(i=0;i<n;i++)
    {
    
    printf("\n\01 Donnez element %i : ",i+1);
    scanf("%i",&element);
    l=ajout_tete(l,element);
    
    }
    return l;
    }
     
    
    /* remplir fin */
    
    liste remplir_fin(liste l)
    {
    	int  n , i , element;
    	
    	while( n <= 0 )
    	{
    printf("\n \02 Donnez nbr element a inserer par fin : ?\b ");
    scanf("%i",&n);
    	}
    
    for(i=0;i<n;i++)
    {
    
    printf("\n\01 Donnez element %i : ",i+1);
    scanf("%i",&element);
    l=ajout_fin(l,element);
    
    }
    return l;
    }
    
    
    void afficher(liste l)
    {
    liste p = l;
    
    printf("\n=======================================================\n");
    
    while( p != NULL)
    {
    
    	printf("%i -> ",p->nbr);
    	p = suivant( p ); 
    
    
    }
    printf("NULL");
    printf("\n=======================================================\n");
    
    
    }
    
    
     /* first solution */ 
    
    /* supprimer element  ( succe = -1 ) */
    /*
    liste supprimer(liste l,int element)
    {
    
    	liste p;
    
     p = rechercher ( l , element );
    
    if( p == NULL ){printf("\n SORRY NO ELEMENT ! \n");return l;} // si l'lelement n'existe pas 
    
    else if( p->suivant == NULL ) { p = NULL; } // si l'element est le debut   
    
    //sinon
    
    else
    {
    
    	p = p->suivant ;
    
    }
    
    return l;
    
    }
    */
    
    
     /* second solution */ 
    /* supprimer element  ( succe = 1 ) !! */
    
    liste supprimer(liste l, int elm)
    {
    
    liste r,p, p1, p2;  
    p1=p2=p=l;
    p2=p2->suivant;
    // si l'element nexiste pas 
    
    if ( rechercher( l , elm ) == NULL ) {printf("\n\02 SORRY NO ELEMENT !\n");return l;} 
    
    // dans le cas ou l'element a supprimer est la tete de la liste 
    if ( l->nbr == elm ) 
    {
    	l=l->suivant;  return l;
    }
    
    // dans le cas ou l'element a supprimer est la queue de la liste 
    if ( elm == fin(l) ) 
    {
    while(p2!=NULL)
    	{
     	 p2=p2->suivant;
    	 p1=p1->suivant;	
    	}
    p1->suivant=NULL;
    }
    
    
    // sinon
    while(p!=NULL)
    {
    	if ( p->nbr == precedent(l,elm) )
    	{
    		p->suivant=p->suivant->suivant;
    		return l;
    	}
    
    p=p->suivant;
    }
    }
    
    
    int main()
    {
    
    int elm;
    liste head,tail;
    
    /* affectation nouvelles listes */
    head = nouveau();
    tail = nouveau();
    
    /* remplissage listes */
    head = remplir_tete( head );
    tail = remplir_fin( tail );
    
    /* affichage listes */
    printf("Afficher head :\n ");
    afficher(head);
    printf("Afficher tail :\n ");
    afficher(tail);
    
    /* suppression element */
    printf("Donnez elemnet a supprimer \n");
    scanf("%i",&elm);
    tail = supprimer( tail, elm );
    printf("afficher apres suppression ");
    afficher( tail ); 
    
    return 0;
    }

  8. #8
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    Code:
     
    if any one have an optimisation 
    please write it and if any function is worng just say me because i'm a beginner ....
    sorry about long post

  9. #9
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159

    Talking take it easy

    the wrong solution is :

    Code:
    /* first solution */ 
    
    /* supprimer element  ( succe = -1 ) */
    /*
    liste supprimer(liste l,int element)
    {
    
    	liste p;
    
     p = rechercher ( l , element );
    
    if( p == NULL ){printf("\n SORRY NO ELEMENT ! \n");return l;} // si l'lelement n'existe pas 
    
    else if( p->suivant == NULL ) { p = NULL; } // si l'element est le debut   
    
    //sinon
    
    else
    {
    
    	p = p->suivant ;
    
    }
    
    return l;
    
    }
    ------------------------------------------------

    take it easy .....
    -----------------------------------------------
    Last edited by enjoy; 01-09-2005 at 11:46 AM. Reason: code

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You're still not indenting your code properly.

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Apparently Santa didn't give him a copy of gnu indent for Christmas...

    /

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  4. lites (debug error )
    By enjoy in forum C Programming
    Replies: 10
    Last Post: 01-08-2005, 10:18 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM