Help with linked list (and reading user input)

This is a discussion on Help with linked list (and reading user input) within the C Programming forums, part of the General Programming Boards category; Hi i have two basic functions Code: llnode* createList(){ char tmp; llnode *head = NULL; if ((head =(llnode *) malloc(sizeof(llnode))) ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    Help with linked list (and reading user input)

    Hi i have two basic functions

    Code:
    llnode* createList(){
     
     char tmp;
     llnode *head = NULL;
     if ((head =(llnode *) malloc(sizeof(llnode))) == NULL) {
          (void)exit();
        }  
     while((tmp = getchar()) != '\n'){
        add(&head,(int)(tmp-'0'));
        }
        
     return head;
    }
    that creates a list by reading in user input and then calling add which adds it to the list

    add:

    Code:
    void add(llnode **head, int data_in) {
        llnode *tmp;
        if ((tmp =(llnode*) malloc(sizeof(llnode))) == NULL) {
          (void)exit();
        }
        tmp->digit = data_in;
        tmp->next = *head;
        *head = tmp;
      }
    For some reason when i run this I ask the user to input a number so say they type in "2221" it will add everythingto the list however it adds a zero aswell for some reason. So my lists is
    "12220" when i print it out. Any ideas? Thx.

  2. #2
    Registered User wintellect's Avatar
    Join Date
    Mar 2006
    Posts
    24
    perhaps you could try to wrap them all in one function (untested code):

    Code:
    llnode *head = NULL;
    
    void create_list()
    {
        llnode *tmp;
        llnode *ptr;
        char c;
    
        while( (c = getchar()) != '\n' && c != EOF )
        {
            if ( (tmp = malloc( sizeof(llnode) )) == NULL)
                exit();
            tmp->digit = c;
            tmp->next = NULL;
    
            ptr = head;
            if (ptr == NULL)
            {
                ptr = tmp;
            }
            else
            {
                while( ptr->next != NULL )
                    ptr = ptr->next;
    
                ptr->next = tmp;
            }
        }
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > So my lists is "12220" when i print it out. Any ideas?
    Yes, it's the dummy (and useless) node you add at the start.

    All you need is
    Code:
     llnode *head = NULL;
     while((tmp = getchar()) != '\n'){
        add(&head,(int)(tmp-'0'));
        }
    Oh, and drop all those casts of malloc - see the FAQ for why.

    I don't know why you're casting the "result" of exit() either, since it doesn't return.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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