Thread: separting integer from string and storing it in a linked list.

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    43

    separting integer from string and storing it in a linked list.

    I wrote a code that takes input from a file and stores them into a linked list.
    My code stores the contents of the file into a node contains a char array.
    So if I did everything right each name in the input file has it's own node and they are linked together.
    However I want to separate the name and store it into a char array and separate the age and store it into an integer array.
    For example Martha will be stored in the first node of the char array. Her age which is 12 will be stored in the int array of the first node. And I want to do it for all the other names.
    This is what I have accomplished so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    #define MAXN 50    
    #define val 2
    typedef struct node {
        char name[MAXN];
        //int age[val];
        struct node *next;
    }
    node;
    
    int main (int argc, char **argv) {
    
        FILE *file = argc > 1 ? fopen (argv[1], "r") : stdin;
        if (file == NULL)
            return 1;
    
        char buf[MAXN];
       // int buf2[val];
       
        node *first = NULL, *last = NULL;   
    
    
        while (fgets (buf, MAXN, file)) {
    
            node *head = malloc (sizeof(node));
            if (head == NULL) {         
                perror ("malloc-node"); 
                return 1;   
            }
    
           
            buf[strcspn(buf, "\n")] = 0;    
            
           
            strcpy (head->name, buf);
            head->next = NULL;
    
      
            if (!last)
                first = last = head;
            else {
                last->next = head;
                last = head;
            }
            
            
        }
    
        if (file != stdin)  
            fclose(file);
    
        node *ptr = first;              
        while (ptr != NULL) {           
            node *node_t = ptr;         
            printf ("%s\n", ptr->name); 
            ptr = ptr->next;            
            free (node_t);              
        }
    
        return 0;
    }
    I don't where or how to edit my code for it to do what I want it to.

    input:
    Code:
    Martha 12
    Bill 33
    Max 78
    Jonathan 12
    Luke 10
    Billy 16
    Robert 21
    Susan 25
    Nathan 20
    Sarah 22

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Add more functions!
    Learn to split bits of functionality into functions to stop your main bloating.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXN 50
    
    typedef struct node {
        char name[MAXN];
        int age;
        struct node *next;
    } node;
    typedef struct list {
        node    *head;
        node    *tail;
    } list;
    
    node *newNode() {
        node *new = malloc(sizeof(*new));
        if ( new ) {
            new->next = NULL;
        }
        return new;
    }
    
    void appendToList(list *root, node *new) {
        if ( root->head == NULL ) {
            root->head = root->tail = new;
        } else {
            root->tail->next = new;
            root->tail = new;
        }
    }
    
    void parseLine(list *root, char inputbuff[] ) {
        char name[MAXN];
        int age;
        if ( sscanf(inputbuff,"%s %d",name,&age) == 2 ) {
            node *new = newNode();
            if ( new ) {
                strcpy(new->name,name);
                new->age = age;
                appendToList(root,new);
            }
        } else {
            fprintf(stderr,"Unable to parse %s",inputbuff);
        }
    }
    
    int main (int argc, char **argv) {
    
        FILE *file = argc > 1 ? fopen (argv[1], "r") : stdin;
        if (file == NULL)
            return 1;
    
        char buf[MAXN];
        list myList = { NULL, NULL };
    
        while (fgets (buf, MAXN, file)) {
            parseLine(&myList,buf);
        }
    
        if (file != stdin)
            fclose(file);
    
        node *ptr = myList.head;
        while (ptr != NULL) {
            node *node_t = ptr;
            printf ("%s is %d years old\n", ptr->name,ptr->age);
            ptr = ptr->next;
            free (node_t);
        }
    
        return 0;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding storing items into a linked list.
    By jsuite in forum C Programming
    Replies: 10
    Last Post: 11-29-2012, 11:03 AM
  2. Storing Sentences in Linked List
    By ncode in forum C Programming
    Replies: 3
    Last Post: 01-31-2012, 12:55 PM
  3. [help] - storing data from txt in linked list
    By Springy in forum C Programming
    Replies: 3
    Last Post: 10-17-2009, 09:14 AM
  4. Storing strings in a linked list
    By dws90 in forum C Programming
    Replies: 1
    Last Post: 02-21-2009, 07:06 PM
  5. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM

Tags for this Thread