Thread: Help with chained lists

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    Help with chained lists

    hello everyone
    i have to do a "text editor" or at least something that types lol
    im learning pointers and we have to make this using lots of pointers
    and it has to be in C so no <string.h> or any other headers like that

    but so far i have a problem

    before you look at the code
    the part that doesnt work is this function
    void add_char( struct c_string **p_str, char letra );


    this is a simpler veersion of my code some parts are not in it
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <ctype.h>
    #define front 2
    
    struct c_string
    {
        struct c_string *ant;
        char letra;
        struct c_string *prox;
    };
    
    struct tp_lin
    {
        struct tp_lin *ant;
        int nro;
        struct c_string  *inicio;
        struct tp_lin *prox;
    };
    
    void init_text( struct tp_lin **p_lin )
    {
        p_lin = NULL;
    }
    
    void add_lin( struct tp_lin **p_lin )
    {
        struct tp_lin *nova, *aux;
        if( *p_lin == NULL )
        {
            nova = (struct tp_lin*) malloc(sizeof(struct tp_lin));
            nova->ant = NULL; 
            nova->nro = 0;
            nova->inicio = NULL;
            nova->prox = NULL;
            *p_lin = nova;          
        }
        else
        {
            aux = *p_lin;
            while( aux->prox != NULL )
                aux = aux->prox;
            nova = (struct tp_lin*) malloc(sizeof(struct tp_lin));
            nova->ant = aux;
            nova->nro = 0;
            nova->inicio = NULL;
            nova->prox = NULL;
            aux->prox = nova;
        }
    }
    
    //BAD FUNCTION
    void add_char( struct c_string **p_str, char letra )
    {
        struct c_string *nova, *aux;
        if( *p_str = NULL )
        {
            nova = (struct c_string*) malloc(sizeof(struct c_string));
            nova->ant = NULL;
            nova->letra = letra;
            nova->prox = NULL;
            *p_str = nova;
        }
        else
        {
            aux = *p_str;
            while( aux->prox != NULL )
                aux = aux->prox;
            nova = (struct c_string*) malloc(sizeof(struct c_string));
            nova->ant = aux;
            nova->letra = letra;
            nova->prox = NULL;
            aux->prox = nova;
        }
    }
    
    int main()
    {
        struct tp_lin *p_ini;
        struct tp_lin *p_lin;
        struct c_string *p_cur;
        char chOpt, chOpt2;
        
        init_text( &p_ini );    // Inicializa o ponteiro p/ NULL 
     
        do
        {
            chOpt = getch();
            if( chOpt == 0 )
                chOpt2 = getch();
            if( isalpha( chOpt ) )  
            {   
    
                if( p_ini == NULL ) 
                {
                    add_lin( &p_ini );   
                    p_lin = p_ini; 
                    add_char( &p_lin->inicio, chOpt );  // THIS IS WHERE MY PROBLEM IS 
                    p_cur = p_lin->inicio;  
                }
                /*else 
                {
                    add_char( p_lin->inicio, chOpt );        
                }*/
                printf("%c",p_cur->letra);
            }
            
        }while( chOpt != 27 );
    
    return 0;
    }
    if your willing to help me but need more info
    just tell me what you want to know

    and thanks a lot in advanced
    Last edited by sh4d0w; 08-21-2010 at 05:52 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What does "doesn't work" mean?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    74
    on the bad function: if( *p_str = NULL )

    you mean == (equal)?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Maybe you should enable your compiler warning and pay attention to them.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    thanks that was a stupid mistake =/
    sorry
    I really didn't see it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Lists? Is it possible? Is it a logical solution?
    By arcaine01 in forum C++ Programming
    Replies: 10
    Last Post: 07-23-2009, 01:08 AM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. Chained hash tables using an array of lists
    By misswaleleia in forum C Programming
    Replies: 2
    Last Post: 05-26-2003, 09:33 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM