Regarding storing items into a linked list.

This is a discussion on Regarding storing items into a linked list. within the C Programming forums, part of the General Programming Boards category; Just a quick question. I recently finished an assignment regarding reading from a file and storing it into a linked ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    48

    Regarding storing items into a linked list.

    Just a quick question. I recently finished an assignment regarding reading from a file and storing it into a linked list. I got it to work, but don't really understand the theory behind it, so the question is this: Does anyone have any info / resources over how to read from a file, and store it into a linked list properly?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,754
    There should be plenty of online resources for reading from files and storing into a linked list. Once you understand both of them, combining the two ideas should be quite straightforward.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    I literally can't find any resources. The only thing I can find is people post there codes only, with help that is inconclusive regarding the functionality of the code. I'm looking for an efficient, correct way of doing this.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    As laserlight has already told you the task has two main parts: reading data from a file and storing the data in a linked list.

    So what part do you not understand? The more specific your question is, the more specific the answers will be.

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    Well you answered the question by posting some resources, so thank you. And I don't understand storing things into linked lists as a whole. While I got it to work, I hardly understand. I guess to be more specific, I'm not sure how exactly I'm storing into it, and also I'm confused about the pointers that go to each node, and manipulating them. As silly as it may sound, I've searched all over for some good resources but couldn't find anything besides something on eternally confused which has helped a lot. Still open for more resources or anyone that want's to add a word in on the subject of linked lists and storing into them via file parsing, Thanks.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    Ok here's a good question. So this is the tutorial on this site for traversing a linked list:

    Code:
    #include <stdio.h>
    #include <stdlib.h>struct node {  int x;  struct node *next;};int main(){    /* This won't change, or we would lose the list in memory */    struct node *root;           /* This will point to each node as it traverses the list */    struct node *conductor;      root = malloc( sizeof(struct node) );      root->next = 0;       root->x = 12;    conductor = root;     if ( conductor != 0 ) {        while ( conductor->next != 0)        {            conductor = conductor->next;        }    }    /* Creates a node at the end of the list */    conductor->next = malloc( sizeof(struct node) );      conductor = conductor->next;     if ( conductor == 0 )    {        printf( "Out of memory" );        return 0;    }    /* initialize the new memory */    conductor->next = 0;             conductor->x = 42;    return 0; }
    But what if you have this:

    Code:
    struct node {
      char string[80];  struct node *next; };
    I put that in the struct, but then pointing to it in the example like this:

    Code:
    root->string = 12;
    Does not work. I'm replacing the int x with the char[80];

    Any ideas?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    EDIT: Please make sure your posts are legible when you're done. You can preview the post before you submit. After you submit, take a quick glance and make sure it's not ugly. Go back and edit the post to fix anything that doesn't look right.

    Of course it doesn't work, why would it? You are trying to assign a single int to an entire char array. Some possible options depending on what you want to do:
    Code:
    root->string[42] = 12;  // set the 43rd element of the array to 12
    strcpy(root->string, "12");  // copy the string "12" into the array
    The data element of the list node is no longer an int, so you can't treat it as one. You must treat it as the char array that it is.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    I'm sorry im at school right now and the connection went out. Ok so I understand how to assign things to the nodes, but how do I read properly from the file and store it?

    For starters, I use fgetsf and a line buffer, right? But then one I read it line by line, how does it distinguish between a string, then say a couple of numbers all separate values, but in one node?

    Any examples?

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    That completely depends on your data.

    Assume you have a text file containing a number, a string and a double on each line:
    Code:
    1 alpha 1.23
    2 beta 2.34
    3 gamma 3.45
    Now you need a node structure which is able to hold one line of data:
    Code:
    struct node
    {
        int number;
        char[10] string;
        double real_number;
        struct node *next;
    };
    For reading the data you have several options: fscanf(), fgets() + sscanf(), fgets() + your own parsing, fgetc().
    Using the combination fgets() + sscanf() you could write something like this in a loop (I'm omitting error checking for brevity, but don't do that in real code!):
    Code:
    struct node *temp = malloc(sizeof(*temp));   // you need a new node for each line
    fgets(buffer, sizeof(buffer), file_pointer);
    sscanf(buffer, "%d %9s %lf", &temp->number, temp->string, &temp->real_number);
    temp->next = NULL;
    Now "temp" contains the data for one line and you are able to insert it into your list.

    Bye, Andreas
    Last edited by AndiPersti; 11-28-2012 at 12:06 PM. Reason: missing &-operator

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    How do you use fscanf when there's a space in the string?

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    Nevermind, I'm using fgets. Still having some minor problems with the print out loop and the read through file loop both of which pertain to the full file not being printed out line by line. However, I am on the right track because the first line prints out perfectly. Once I find the solution, I will make a dummy version of my work and post it here for documentation, just because when searching around for code over this topic, majority of the hits are horridly inefficient.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List for items???
    By voidpain() in forum C Programming
    Replies: 7
    Last Post: 08-08-2011, 09:36 PM
  2. how to reverse items in linked list?
    By ilikeapplepie in forum C Programming
    Replies: 8
    Last Post: 04-09-2011, 11:15 PM
  3. Delete All Items In A Linked List
    By Khellendros in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2009, 12:22 AM
  4. modifying items in a linked list?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-14-2002, 01:37 PM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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