Search code examples
clinked-listcharsingly-linked-listgetchar

Scan chars from user linked list of


I'm supposed to write a function that scan input from user till enter and enter it to linked list. I cant scan it into a String and than put it in a list.

I wrote this code and doesn't enter the chars to linked list and doesn't exit from the console till I press Exit.

typedef struct chNode
{
    char data;
    struct chNode *next;
}chNode;


chNode * createCharList(char data)
{
    chNode *temp = (chNode*)malloc(sizeof(chNode));
    temp->data = data;
    temp->next = NULL;
    return temp;
}

chNode * addCharToLast(chNode *head, char data)
{
    chNode *p = head;
    chNode *temp = createCharList(data);
    if (head == NULL)
        return temp;
    while (p->next != NULL)
        p = p->next;
    p->next = temp;
    return head;
}


chNode* insert_Charlist() 
{
    char ch;
    chNode *Head = NULL;
    printf("Enter chars For Linked-List Till 'Enter':\n");
    scanf_s("%c", &ch);
    while (ch != '\n')
    {

        Head = addCharToLast(Head, ch); 
        scanf_s("%c", &ch);
        
    }
    return Head;
}

void main()
{

    chNode *Orignial_list = NULL;
    Orignial_list = insert_Charlist(); // Function that imports a list
    printf("You Entered This linked-list:\n");
    printf_CharList(Orignial_list); //Printing the linked list
    
    getch();

}

Solution

  • For this call of scanf

    scanf_s("%c", &ch);
    

    you need to specify one more argument

    scanf_s( "%c", &ch, 1 );
    

    Your approach of the implementation of the function insert_Charlist with calls of the function addCharToLast is inefficient because the function addCharToLast will traverse the whole list each time when a new character is added to the list.

    I can suggest the following function implementation as it is shown in the demonstrative program below.

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct chNode
    {
        char data;
        struct chNode *next;
    } chNode;
    
    chNode * createNode(char data )
    {
        chNode *temp = (chNode*)malloc( sizeof( chNode ) );
        
        if ( temp != NULL )
        {
            temp->data = data;
            temp->next = NULL;
        }
        
        return temp;
    }
    
    size_t insert_Charlist( chNode **head ) 
    {
        while ( *head ) head = &( *head )->next;
        
        size_t n = 0;
        
        printf( "Enter chars For Linked-List Till 'Enter': " );
    
        for ( int data; ( data = getchar() ) != EOF && 
                        data != '\n' && 
                        ( *head = createNode( data ) ) != NULL; )
        {
            head = &( *head )->next;
            ++n;
        }
        
        return n;
    }
    
    void printf_CharList( const chNode *head )
    {
        for ( ; head != NULL; head = head->next )
        {
            printf( "%c -> ", head->data );
        }
        
        puts( "null" );
    }
    
    int main(void) 
    {
        chNode *head = NULL;
        
        insert_Charlist( &head );
        
        printf_CharList( head );
        
        return 0;
    }
    

    The program output might look like

    Enter chars For Linked-List Till 'Enter': Hello
    H -> e -> l -> l -> o -> null
    

    Pay attention to that according to the C Standard the function main without parameters shall be declared like

    int main( void )