Linked list; heelp

This is a discussion on Linked list; heelp within the C Programming forums, part of the General Programming Boards category; How do I continue reading in input even if it encounters a space? Is it possible to get several sentences, ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    3

    Question Linked list; heelp

    How do I continue reading in input even if it encounters a space? Is it possible to get several sentences, save it in a linked list , then output it? In my program everytime a space is encountered, it prints out the input one word at a time, each one a newline, instead of a sentece.
    How do I modify it to accept sentences instead of words?
    Thanks in advance!

    #include <stdio.h>
    #include <stdlib.h>

    #define MAXNAMELEN 20

    typedef struct bdaynode
    {
    char fname[MAXNAMELEN];
    struct namenode *next;
    } NameStruct;

    typedef NameStruct *NamePtr;

    NamePtr newName(void)
    {
    NamePtr element;

    element = (NamePtr) malloc(sizeof(NameStruct));
    element->next = NULL;
    return(element);
    }

    void setNameData(NamePtr list, char *fname)
    {
    strcpy(list->fname, fname);

    return;
    }

    NamePtr getName(void)
    {
    char fname[MAXNAMELEN];
    NamePtr tmp;

    printf("Enter person name: ");
    if (scanf("%s", fname) == EOF)
    return(NULL);

    tmp = newName();
    setNameData(tmp, fname);
    return(tmp);
    }

    NamePtr insert(NamePtr list, NamePtr element)
    {
    element->next = list;
    return(element);
    }

    NamePtr insertEnd(NamePtr list, NamePtr element)
    {
    NamePtr tmp;

    if (list == NULL)
    return(element);
    for(tmp = list; tmp->next != NULL; tmp = tmp->next)
    ;
    tmp->next = element;
    return(list);
    }

    void printName(NamePtr list)
    {
    NamePtr tmp;

    printf("\n");
    for(tmp = list; tmp != NULL; tmp = tmp->next)
    printf("%s\n", tmp->fname);
    return;
    }

    NamePtr deleteAll(NamePtr list)
    {
    NamePtr tmp;
    for(; list != NULL; list = tmp)
    {
    tmp = list->next;
    free(list);
    }
    return(NULL);
    }


    int main(void)
    {
    NamePtr firstName = NULL, tmp;

    while ((tmp = getName()) != NULL)
    firstName = insertEnd(firstName, tmp);

    printName(firstName);

    deleteAll(firstName);

    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    NamePtr getName(void) 
    { 
        char fname[MAXNAMELEN]; 
        NamePtr tmp; 
    
        printf("Enter person name: "); 
        if (scanf("%s", fname) == EOF) 
            return(NULL); 
    
        tmp = newName(); 
        setNameData(tmp, fname); 
        return(tmp); 
    }
    1) Increase 'MAXNAMELEN'.
    2) Use something similar to fgets to read.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 09:04 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21